1.Program to calculate simple interest?
#include <iostream>
using namespace std;
int main () {
float principle, rate, time, simpleInterest;
// Input principle amount, rate of interest, and time period
cout << "Enter principle amount: ";
cin >> principle;
cout << "Enter rate of interest (in percentage): ";
cin >> rate;
cout << "Enter time period (in years): ";
cin >> time;
// Calculate simple interest
simpleInterest = (principle * rate * time) / 100;
// Output the result
cout << "Simple Interest = " << simpleInterest << endl;
return 0;
}
2.Program to check whether number is EVEN
or ODD?
#include <iostream>
using namespace std;
int main() {
int number;
// Input the number
cout << "Enter an integer: ";
cin >> number;
// Check if the number is even or odd
if (number % 2 == 0) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}
return 0;
}
3.Program to read marks and print
percentage and division?
#include <iostream>
using namespace std;
int main() {
int marks[5], totalMarks = 0;
float percentage;
// Input marks for 5 subjects
cout << "Enter marks for 5 subjects:" << endl;
for (int i = 0; i < 5; ++i) {
cout << "Subject " << i + 1 << ": ";
cin >> marks[i];
totalMarks += marks[i];
}
percentage = static_cast<float>(totalMarks) / 5.0;
cout << "Percentage: " << percentage << "%" << endl;
// Determine division
if (percentage >= 60) {
cout << "Division: First" << endl;
} else if (percentage >= 50) {
cout << "Division: Second" << endl;
} else if (percentage >= 40) {
cout << "Division: Third" << endl;
} else {
cout << "Division: Fail" << endl;
}
return 0;
}
7. How to swap two numbers without using
a temporary variable?
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers:" << endl;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
4.Program to find gross salary of an
employee?
#include <iostream>
using namespace std;
int main() {
float basicSalary, grossSalary, da, hra;
// Input basic salary
cout << "Enter basic salary of the employee: ";
cin >> basicSalary;
// Calculate Dearness Allowance (DA) and House Rent Allowance
(HRA)
da = 0.4 * basicSalary;
hra = 0.2 * basicSalary;
// Calculate gross salary
grossSalary = basicSalary + da + hra;
// Output the result
cout << "Dearness Allowance (DA): " << da << endl;
cout << "House Rent Allowance (HRA): " << hra << endl;
cout << "Gross Salary: " << grossSalary << endl;
return 0;
}
6. Program to find area and perimeter of the
rectangle
#include <iostream>
using namespace std;
int main() {
float length, width, area, perimeter;
// Input length and width of the rectangle
cout << "Enter length of the rectangle: ";
cin >> length;
cout << "Enter width of the rectangle: ";
cin >> width;
area = length * width;
perimeter = 2 * (length + width);
// Output the result
cout << "Area of the rectangle: " << area << endl;
cout << "Perimeter of the rectangle: " << perimeter << endl;
return 0;
}
7.Program to find the LCM (Lowest
Common Multiple) of two integers
#include <iostream>
using namespace std;
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2;
// Input two numbers
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
// Calculate LCM and output the result
cout << "LCM of " << num1 << " and " << num2 << " is: " <<
lcm(num1, num2) << endl;
return 0;
}
8. . Program to find the GCD (Greatest
Common Divisor) of two integers
#include <iostream>
using namespace std;
// Function to find the GCD (Greatest Common Divisor) using
Euclidean algorithm
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2;
// Input two numbers
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
// Calculate GCD and output the result
cout << "GCD of " << num1 << " and " << num2 << " is: " <<
gcd(num1, num2) << endl;
return 0;
}
9. Program to calculate the addition of
two complex numbers
#include <iostream>
using namespace std;
struct Complex {
float real;
float imag;
};
Complex addComplex(Complex num1, Complex num2) {
Complex result;
result.real = num1.real + num2.real;
result.imag = num1.imag + num2.imag;
return result;
}
int main() {
Complex num1, num2, sum;
cout << "Enter real and imaginary parts of first complex number:"
<< endl;
cout << "Real part: ";
cin >> num1.real;
cout << "Imaginary part: ";
cin >> num1.imag;
cout << "Enter real and imaginary parts of second complex
number:" << endl;
cout << "Real part: ";
cin >> num2.real;
cout << "Imaginary part: ";
cin >> num2.imag;
sum = addComplex(num1, num2);
cout << "Sum of the two complex numbers: " << sum.real << " + "
<< sum.imag << "i" << endl;
return 0;}
10.program to print tables from numbers 1
to 20?
#include <iostream>
using namespace std;
int main() {
// Loop through numbers from 1 to 20
for (int i = 1; i <= 20; ++i) {
cout << "Multiplication table of " << i << ":" << endl;
// Print the multiplication table for the current number
for (int j = 1; j <= 10; ++j) {
cout << i << " * " << j << " = " << (i * j) << endl;
cout << endl;
return 0;
}
11.Program to print following Pyramid: * * *
************
#include <iostream>
using namespace std;
int main() {
int rows;
// Input number of rows
cout << "Enter the number of rows: ";
cin >> rows;
// Outer loop for number of rows
for (int i = rows; i >= 1; --i) {
// Inner loop for printing '*' in each row
for (int j = 1; j <= i; ++j) {
cout << "* ";
cout << endl;
return 0;
}
12. Program to print following Pyramid: *
**************
#include <iostream>
using namespace std;
int main() {
int rows;
// Input number of rows
cout << "Enter the number of rows: ";
cin >> rows;
// Outer loop for number of rows
for (int i = 1; i <= rows; ++i) {
// Inner loop for printing '*' in each row
for (int j = 1; j <= i; ++j) {
cout << "* ";
cout << endl;
return 0;
}
13.Program to find multiplication of two
matrices?
#include <iostream>
using namespace std;
const int MAX = 100;
void multiplyMatrix(int mat1[][MAX], int mat2[][MAX], int result[][MAX], int rows1, int
cols1, int cols2) {
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
result[i][j] = 0 } }
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
for (int k = 0; k < cols1; ++k) {
result[i][j] += mat1[i][k] * mat2[k][j]; } } } }
int main() {
int mat1[MAX][MAX], mat2[MAX][MAX], result[MAX][MAX];
int rows1, cols1, rows2, cols2;
cout << "Enter number of rows and columns of first matrix: ";
cin >> rows1 >> cols1;
cout << "Enter elements of first matrix:" << endl;
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols1; ++j) {
cin >> mat1[i][j]; }}
cout << "Enter number of rows and columns of second matrix: ";
cin >> rows2 >> cols2;
cout << "Enter elements of second matrix:" << endl;
for (int i = 0; i < rows2; ++i) {
for (int j = 0; j < cols2; ++j) {
cin >> mat2[i][j];
if (cols1 != rows2) {
cout << "Matrices cannot be multiplied!" << endl;
return 0;
multiplyMatrix(mat1, mat2, result, rows1, cols1, cols2);
cout << "Resultant matrix after multiplication:" << endl;
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
cout << result[i][j] << " ";
cout << endl;
return 0;
}
14. Program to demonstrate multi-
level Inheritance?
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Animal is eating." << endl;
}};
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking." << endl;
}
};
class Labrador : public Dog {
public:
void color() {
cout << "Labrador is golden in color." << endl;
}
};
int main() {
Labrador lab;
lab.eat();
lab.bark();
lab.color();
return 0;
}
15.program to demonstrate
Constructor and Destructor?
#include <iostream>
using namespace std;
// Class definition
class MyClass {
public:
// Constructor
MyClass() {
cout << "Constructor called" << endl;
}
// Destructor
~MyClass() {
cout << "Destructor called" << endl;
}
};
int main() {
// Creating an object of MyClass
MyClass obj;
// Program execution ends, object goes out of scope
return 0;
}
16. Program to demonstrate Function
Overloading?
#include <iostream>
using namespace std;
// Function to add two integers
int add(int a, int b) {
return a + b;
}
// Function to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
// Function to add two floats
float add(float a, float b) {
return a + b;
}
int main() {
int sum1, sum2;
float sum3;
// Call the overloaded functions
sum1 = add(5, 3);
sum2 = add(4, 7, 2);
sum3 = add(2.5f, 3.5f);
cout << "Sum of 5 and 3: " << sum1 << endl;
cout << "Sum of 4, 7, and 2: " << sum2 << endl;
cout << "Sum of 2.5 and 3.5: " << sum3 << endl;
return 0;
}
17. . Program to demonstrate Friend
function?
#include <iostream>
using namespace std;
// Forward declaration of the class
class MyClass;
void showValue(MyClass obj);
class MyClass {
private:
int value;
public:
MyClass(int val) : value(val) {}
// Friend declaration
friend void showValue(MyClass obj);
};
// Friend function definition
void showValue(MyClass obj) {
// Accessing private member of MyClass
cout << "Value in MyClass: " << obj.value << endl;
}
int main() {
// Create an object of MyClass
MyClass obj(10);
// Call the friend function
showValue(obj);
return 0;
}
18. Program to create, open and close a file?
#include <iostream>
#include <fstream> // Header file for file handling
using namespace std;
int main() {
ofstream outputFile; // Create an object of ofstream for writing to a file
// Open a file named "example.txt" for writing
outputFile.open("example.txt");
// Check if the file is successfully opened
if (!outputFile) {
cout << "Error: Unable to open file!" << endl;
return 1; // Exit the program with an error status
outputFile << "Hello, this is some text written to the file." << endl;
outputFile.close();
cout << "File created, written, and closed successfully." << endl;
return 0;