Basic Practice Problems For Beginners In c++
Problem 1: Calculate the area of a rectangle.
cpp
#include<iostream>
using namespace std;
int main() {
double length, width, area;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
area = length * width;
cout << "Area of the rectangle is: " << area << endl;
return 0;
}
Problem 2: Check if a number is even or odd.
cpp#include<iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
return 0;
}
Problem 3: Find the maximum and minimum of an array.
cpp#include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int max = arr[0];
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
cout << "Maximum element: " << max << endl;
cout << "Minimum element: " << min << endl;
return 0;
}
Problem 4: Check if a number is prime.
cpp#include<iostream>
using namespace std;
bool isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPrime(num)) {
cout << num << " is a prime number." << endl;
} else {
cout << num << " is not a prime number." << endl;
}
return 0;
}
Problem 5: Calculate the factorial of a number using recursion.
cpp#include<iostream>
using namespace std;
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factorial of " << num << " is: " << factorial(num) << endl;
return 0;
}
Problem 6: Check if a year is a leap year.
cpp#include<iostream>
using namespace std;
bool isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if (isLeapYear(year)) {
cout << year << " is a leap year." << endl;
} else {
cout << year << " is not a leap year." << endl;
}
return 0;
}
Problem 7: Find the sum of the digits of a number.
cpp#include<iostream>
using namespace std;
int sumOfDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Sum of the digits of " << num << " is: " << sumOfDigits(num) << endl;
return 0;
}
Problem 8: Check if a number is a palindrome.
cpp#include<iostream>
using namespace std;
bool isPalindrome(int num) {
int originalNum = num;
int reverseNum = 0;
while (num > 0) {
reverseNum = reverseNum * 10 + num % 10;
num /= 10;
}
return originalNum == reverseNum;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPalindrome(num)) {
cout << num << " is a palindrome number." << endl;
} else {
cout << num << " is not a palindrome number." << endl;
}
return 0;
}
Problem 9: Find the sum of an arithmetic series.
cpp#include<iostream>
using namespace std;
int main() {
int n, firstTerm, commonDifference, sum;
cout << "Enter the number of terms in the series: ";
cin >> n;
cout << "Enter the first term of the series: ";
cin >> firstTerm;
cout << "Enter the common difference: ";
cin >> commonDifference;
sum = (n * (2 * firstTerm + (n - 1) * commonDifference)) / 2;
cout << "Sum of the arithmetic series is: " << sum << endl;
return 0;
}
Problem 10: Check if a string is a palindrome.
cpp#include<iostream>
using namespace std;
bool isPalindrome(string str) {
int i = 0;
int j = str.length() - 1;
while (i < j) {
if (str[i] != str[j]) {
return false;
}
i++;
j--;
}
return true;
}
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
if (isPalindrome(str)) {
cout << str << " is a palindrome." << endl;
} else {
cout << str << " is not a palindrome." << endl;
}
return 0;
}
Problem 11: Find the sum of elements in an array.
cpp#include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
cout << "Sum of the elements in the array is: " << sum << endl;
return 0;
}
Problem 12: Find the reverse of a number.
cpp#include<iostream>
using namespace std;
int reverseNumber(int num) {
int reverseNum = 0;
while (num > 0) {
reverseNum = reverseNum * 10 + num % 10;
num /= 10;
}
return reverseNum;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Reverse of " << num << " is: " << reverseNumber(num) << endl;
return 0;
}
Problem 13: Find the power of a number.
cpp#include<iostream>
using namespace std;
int power(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
int main() {
int base, exponent;
cout << "Enter the base number: ";
cin >> base;
cout << "Enter the exponent: ";
cin >> exponent;
cout << base << " raised to the power " << exponent << " is: " << power(base, exponent) << endl;
return 0;
}
Problem 14: Find the largest element in an array.
cpp#include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
cout << "Largest element in the array is: " << max << endl;
return 0;
}
Problem 15: Check if a string is a palindrome (case-insensitive).
cpp#include<iostream>
#include<algorithm>
using namespace std;
bool isPalindrome(string str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
int i = 0;
int j = str.length() - 1;
while (i < j) {
if (str[i] != str[j]) {
return false;
}
i++;
j--;
}
return true;
}
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
if (isPalindrome(str)) {
cout << str << " is a palindrome." << endl;
} else {
cout << str << " is not a palindrome." << endl;
}
return 0;
}
Problem 16: Find the factorial of a number using a while loop.
cpp#include<iostream>
using namespace std;
int factorial(int n) {
int fact = 1;
while (n > 1) {
fact *= n;
n--;
}
return fact;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factorial of " << num << " is: " << factorial(num) << endl;
return 0;
}
Problem 17: Convert temperature from Celsius to Fahrenheit.
cpp#include<iostream>
using namespace std;
double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}
int main() {
double celsius;
cout << "Enter temperature in Celsius: ";
cin >> celsius;
double fahrenheit = celsiusToFahrenheit(celsius);
cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
return 0;
}
Problem 18: Find the length of a string without using the built-in length()
function.
cpp#include<iostream>
using namespace std;
int findLength(string str) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
cout << "Length of the string: " << findLength(str) << endl;
return 0;
}
Problem 19: Swap two numbers without using a third variable.
cpp#include<iostream>
using namespace std;
void swapNumbers(int& num1, int& num2) {
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
}
int main() {
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
swapNumbers(num1, num2);
cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << endl;
return 0;
}
Problem 20: Check if a string is a palindrome (ignoring spaces).
cp#include<iostream>
#include<algorithm>
using namespace std;
bool isPalindrome(string str) {
str.erase(remove_if(str.begin(), str.end(), ::isspace), str.end());
transform(str.begin(), str.end(), str.begin(), ::tolower);
int i = 0;
int j = str.length() - 1;
while (i < j) {
if (str[i] != str[j]) {
return false;
}
i++;
j--;
}
return true;
}
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
if (isPalindrome(str)) {
cout << str << " is a palindrome." << endl;
} else {
cout << str << " is not a palindrome." << endl;
}
return 0;
}
Comments