Add Two Numbers
Question: Write a program to take two numbers as input
and display their sum.
Solution:
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "The sum is: " << num1 + num2 << endl;
return 0;
}
3. Calculate Area of a Rectangle
Question: Write a program to calculate the area of a rectangle. Input length and width from
the user.
Solution:
#include <iostream>
using namespace std;
int main() {
double length, width;
cout << "Enter the length and width of the rectangle: ";
cin >> length >> width;
cout << "The area of the rectangle is: "
<< length * width << endl;
return 0;
}
4. Multiply Two Numbers
Question: Write a program to multiply two numbers.
Solution:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "The product is: " << a * b << endl;
return 0;
}
5. Increment and Decrement a Number
Question: Write a program to demonstrate the increment and decrement operators.
Solution:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Initial value: " << num << endl;
cout << "After increment: " << ++num << endl;
cout << "After decrement: " << --num << endl;
return 0;
}
6. Calculate Simple Interest
Question: Write a program to calculate simple interest using the formula
SI =(principleAmt*rate*time)/100
Solution:
#include <iostream>
using namespace std;
int main() {
double principal, rate, time,simpleInterest;
cout << "Enter principal amount, rate of interest, and time (in years):
";
cin >> principal >> rate >> time;
simpleInterest = (principal * rate * time) / 100;
cout << "The simple interest is: " << simpleInterest << endl;
return 0;
}
7. Convert Celsius to Fahrenheit
Question: Write a program to convert temperature from Celsius to Fahrenheit using the
formula F=9/5C+32
Solution:
#include <iostream>
using namespace std;
int main() {
double celsius;
cout << "Enter temperature in Celsius: ";
cin >> celsius;
double fahrenheit = (9.0 / 5.0) * celsius + 32;
cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
return 0;
}
8. Find Remainder
Question: Write a program to calculate the remainder when one number is divided by
another.
Solution:
#include <iostream>
using namespace std;
int main() {
int dividend, divisor;
cout << "Enter dividend and divisor: ";
cin >> dividend >> divisor;
cout << "The remainder is: " << dividend % divisor << endl;
return 0;
}
9. Find Average of Three Numbers
Question: Write a program to calculate the average of three numbers.
Solution:
#include <iostream>
using namespace std;
int main() {
double num1, num2, num3;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
double average = (num1 + num2 + num3) / 3;
cout << "The average is: " << average << endl;
return 0;
}
10. Swap Two Numbers
Question: Write a program to swap two numbers using a temporary variable.
Solution:
#include <iostream>
using namespace std;
int main() {
int a, b, temp;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Before swapping: a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "After swapping: a = " << a << ", b = " << b << endl;
return 0;
}
Calculate the Area and Perimeter of a Rectangle
Question: Write a program that calculates and displays the area and perimeter of a rectangle.
Take the length and breadth as input.
Solution:
#include <iostream>
using namespace std;
int main() {
double length, breadth, area, perimeter;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the breadth of the rectangle: ";
cin >> breadth;
area = length * breadth;
perimeter = 2 * (length + breadth);
cout << "Area: " << area << endl;
cout << "Perimeter: " << perimeter << endl;
return 0;
}
2. Simple Interest Calculation
Question: Write a program to calculate simple interest using the formula SI = (P * R * T)
/ 100.
Solution:
#include <iostream>
using namespace std;
int main() {
double principal, rate, time, simpleInterest;
cout << "Enter the principal amount: ";
cin >> principal;
cout << "Enter the rate of interest: ";
cin >> rate;
cout << "Enter the time (in years): ";
cin >> time;
simpleInterest = (principal * rate * time) / 100;
cout << "Simple Interest: " << simpleInterest << endl;
return 0;
}
3. Swapping Two Numbers
Question: Write a program to swap the values of two variables using a third variable.
Solution:
#include <iostream>
using namespace std;
int main() {
int a, b, temp;
cout << "Enter the first number: ";
cin >> a;
cout << "Enter the second number: ";
cin >> b;
temp = a; // Save the value of a in temp
a = b; // Assign the value of b to a
b = temp; // Assign the value of temp (original a) to b
cout << "After swapping, first number: " << a << ", second number: " <<
b << endl;
return 0;
}
4. Increment and Decrement Operator
Question: Write a program to demonstrate pre-increment and post-increment.
Solution:
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 5, result1, result2;
result1 = ++a; // Pre-increment
result2 = b++; // Post-increment
cout << "After pre-increment, a: " << a << ", result1: " << result1 <<
endl;
cout << "After post-increment, b: " << b << ", result2: " << result2 <<
endl;
return 0;
}
5. Arithmetic Operations
Question: Write a program to perform addition, subtraction, multiplication, division, and
modulus operations on two integers.
Solution:
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Addition: " << (num1 + num2) << endl;
cout << "Subtraction: " << (num1 - num2) << endl;
cout << "Multiplication: " << (num1 * num2) << endl;
cout << "Division: " << (num1 / num2) << endl;
cout << "Modulus: " << (num1 % num2) << endl;
return 0;
}
6. Operator Precedence
Question: Write a program to demonstrate operator precedence in arithmetic operations.
Solution:
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10, c = 15, result;
result = a + b * c; // Multiplication is performed first
cout << "Result of a + b * c: " << result << endl;
result = (a + b) * c; // Parentheses alter precedence
cout << "Result of (a + b) * c: " << result << endl;
result = a + b / c; // Division is performed first
cout << "Result of a + b / c: " << result << endl;
return 0;
}
7. BMI Calculator
Question: Write a program to calculate the Body Mass Index (BMI) using the formula BMI =
weight / (height * height).
Solution:
#include <iostream>
using namespace std;
int main() {
double weight, height, bmi;
cout << "Enter your weight (in kg): ";
cin >> weight;
cout << "Enter your height (in meters): ";
cin >> height;
bmi = weight / (height * height);
cout << "Your BMI is: " << bmi << endl;
return 0;
}
8. Calculate Remainder without Using % Operator
Question: Write a program to calculate the remainder of two numbers without using the %
operator.
Solution:
#include <iostream>
using namespace std;
int main() {
int dividend, divisor, remainder;
cout << "Enter the dividend: ";
cin >> dividend;
cout << "Enter the divisor: ";
cin >> divisor;
remainder = dividend - (dividend / divisor) * divisor;
cout << "Remainder: " << remainder << endl;
return 0;
}
9. Convert Celsius to Fahrenheit
Question: Write a program to convert a temperature from Celsius to Fahrenheit using the
formula F = (9/5)C + 32.
Solution:
#include <iostream>
using namespace std;
int main() {
double celsius, fahrenheit;
cout << "Enter temperature in Celsius: ";
cin >> celsius;
fahrenheit = (9.0 / 5.0) * celsius + 32;
cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
return 0;
}
10. Calculate Total Marks and Percentage
Question: Write a program to calculate the total marks and percentage of a student for five
subjects.
Solution:
#include <iostream>
using namespace std;
int main() {
double sub1, sub2, sub3, sub4, sub5, total, percentage;
cout << "Enter marks for subject 1: ";
cin >> sub1;
cout << "Enter marks for subject 2: ";
cin >> sub2;
cout << "Enter marks for subject 3: ";
cin >> sub3;
cout << "Enter marks for subject 4: ";
cin >> sub4;
cout << "Enter marks for subject 5: ";
cin >> sub5;
total = sub1 + sub2 + sub3 + sub4 + sub5;
percentage = (total / 500) * 100;
cout << "Total Marks: " << total << endl;
cout << "Percentage: " << percentage << "%" << endl;
return 0;
}