simple_cplus_cplus_programs
simple_cplus_cplus_programs
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
return 0;
}
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;
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;
cout << "After swapping, first number: " << a << ", second number: " <<
b << endl;
return 0;
}
Solution:
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 5, result1, result2;
cout << "After pre-increment, a: " << a << ", result1: " << result1 <<
endl;
cout << "After post-increment, b: " << b << ", result2: " << result2 <<
endl;
return 0;
}
5. Arithmetic Operations
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;
return 0;
}
6. Operator Precedence
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;
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;
cout << "Your BMI is: " << bmi << endl;
return 0;
}
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;
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;
return 0;
}
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;
return 0;
}