Question 1.
C++ program for N numbers to calculate sum and average of
these numbers.
// Type I
#include <iostream>
using namespace std;
int main() {
int N;
double num, sum = 0.0, average;
// Input number of elements
cout << "Enter the number of elements: ";
cin >> N;
// Input numbers and calculate sum
for (int i = 1; i <= N; i++) {
cout << "Enter number " << i << ": ";
cin >> num;
sum += num;
}
// Calculate average
average = sum / N;
// Output results
cout << "\nSum = " << sum << endl;
cout << "Average = " << average << endl;
return 0;
}
// Type II
#include <iostream>
using namespace std;
int main() {
int n;
double num, sum = 0.0, average;
cout << "Enter the number of elements: ";
cin >> n;
if (n <= 0) {
cout << "Invalid number of elements. Please enter a positive integer."
<< endl;
return 1;
}
cout << "Enter " << n << " numbers:" << endl;
for (int i = 0; i < n; i++) {
cin >> num;
sum += num;
}
average = sum / n;
cout << "Sum = " << sum << endl;
cout << "Average = " << average << endl;
return 0;
}
Question 2. C++ program for 3 digit numbers to calculate reverse and
sum numbers.
// Type I
#include <iostream>
using namespace std;
int main() {
int num, reverse = 0, sum = 0, digit;
cout << "Enter a 3-digit number: ";
cin >> num;
// Check if the number is 3-digit
if (num < 100 || num > 999) {
cout << "Error: Please enter a valid 3-digit number." << endl;
return 1;
}
int original = num; // Store original number
// Reverse the number and calculate sum of digits
while (num > 0) {
digit = num % 10; // Get last digit
reverse = reverse * 10 + digit; // Build reverse number
sum += digit; // Add digit to sum
num /= 10; // Remove last digit
}
// Output results
cout << "Original number: " << original << endl;
cout << "Reversed number: " << reverse << endl;
cout << "Sum of digits: " << sum << endl;
return 0;
}
//Type 2
#include <iostream>
using namespace std;
int main() {
int number, originalNumber;
int digit1, digit2, digit3;
int reversedNumber, sumOfDigits;
// Get input from user
cout << "Enter a 3-digit number: ";
cin >> number;
// Validate input is a 3-digit number
if (number < 100 || number > 999) {
cout << "Error: Please enter a valid 3-digit number." << endl;
return 1;
}
originalNumber = number;
// Extract digits
digit1 = number % 10; // Get last digit (units place)
number /= 10; // Remove last digit
digit2 = number % 10; // Get middle digit (tens place)
number /= 10; // Remove middle digit
digit3 = number; // Get first digit (hundreds place)
// Calculate reversed number
reversedNumber = digit1 * 100 + digit2 * 10 + digit3;
// Calculate sum of digits
sumOfDigits = digit1 + digit2 + digit3;
// Display results
cout << "Original number: " << originalNumber << endl;
cout << "Reversed number: " << reversedNumber << endl;
cout << "Sum of digits: " << sumOfDigits << endl;
return 0;
}
Question 3. C++ program to calculate LCM and HCF of 2 numbers
// Type I
#include <iostream>
using namespace std;
// Function to find GCD (HCF) using Euclidean algorithm
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Function to find LCM using formula
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
int hcf = gcd(num1, num2);
int lcm_value = lcm(num1, num2);
cout << "\nHCF (GCD) of " << num1 << " and " << num2 << " = " << hcf <<
endl;
cout << "LCM of " << num1 << " and " << num2 << " = " << lcm_value << endl;
return 0;
}
//Type II
#include <iostream>
using namespace std;
int main() {
int num1, num2, hcf, lcm;
// Input numbers
cout << "Enter two numbers: ";
cin >> num1 >> num2;
int a = num1;
int b = num2;
// Find HCF (Euclidean Algorithm)
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
hcf = a;
// Calculate LCM
lcm = (num1 * num2) / hcf;
// Output results
cout << "HCF (GCD) = " << hcf << endl;
cout << "LCM = " << lcm << endl;
return 0;
}
Question 4. C++ program to check the number is even or odd .
#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;
}
Question 5. C++ program to find perimeter and area of square .
#include <iostream>
using namespace std;
int main() {
double side, area, perimeter;
// Input the side length of the square
cout << "Enter the length of the side of the square: ";
cin >> side;
// Calculate area and perimeter
area = side * side; // Area = side²
perimeter = 4 * side; // Perimeter = 4 × side
// Output results
cout << "Area of the square = " << area << endl;
cout << "Perimeter of the square = " << perimeter << endl;
return 0;
}
Question 6. C++ program to find perimeter and area of circle .
#include <iostream>
using namespace std;
int main() {
double radius, area, perimeter;
const double PI = 3.14159; // Constant value for π
cout << "Enter the radius of the circle: ";
cin >> radius;
area = PI * radius * radius; // Area formula: πr²
perimeter = 2 * PI * radius; // Perimeter formula: 2πr
cout << "\nArea of the circle = " << area << endl;
cout << "Perimeter (Circumference) of the circle = " << perimeter << endl;
return 0;
}
Question 7. C++ program to swapping of numbers
#include <iostream>
using namespace std;
int main() {
int a, b, temp;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
// Swapping using temp variable
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping:" << endl;
cout << "First number = " << a << endl;
cout << "Second number = " << b << endl;
return 0;
}