NAME: Deep Jain
Enroll/GR No: 15594
PROGRAM NAME:- B.TECH CE SEM-III
COURSE CODE: - CE302P
COURSE:- OBJECT ORIENTED PROGRAMMING
PRACTICAL
FACULTY NAME:- MR. BHAVESH CHAVDA
Sr.N
Date Practical Name Sign
o
19.07.202 Write A Program To Take Two Numbers As Input And Print Their
1
4 Sum And Average.
02.08.202 Write A Program To Swap Two Numbers Without Using A Third
2
4 Variable.
09.08.202 Write A Program To Check Whether The Number Provided Is Even
3
4 Or Odd.
16.08.202 Write A Program To Print The Largest Number Among Three
4
4 Numbers Given By The User
23.08.202 Write A Single Program That Provides The Sum, Difference,
5
4 Multiplication And Division Of Two Numbers.
06.09.202
6 Write A Program To Print Table Of Any Number Using A For Loop.
4
06.09.202
7 Write A Program To Print Table Of A Number Using Do While Loop.
4
13.09.202 Write A Program To Print Fibonacci Series (0, 1, 1, 2, 3, 5, 8, 13,
8
4 21,...).
13.09.202
9 Write A Program To Reverse A Number.
4
20.09.202 Write A Program To Check Whether A Number Is A Prime Number
10
4 Or Not.
20.09.202
11 Write A Program To Convert Binary Number To Decimal Number
4
20.09.202 Write A Program That Takes Values In An Array And Also Display
12
4 Them.
27.09.202
13 Write A Program To Print Length Of A String Provided
4
27.09.202
14 Write A Program To Check Whether A String Is Palindrome Or Not
4
27.09.202
15 Write A Program That Provides The Sum Of Two Matrices
4
04.10.202
16 Write A Program To Find Out The Product Of Two Matrices
4
04.10.202 Write A Program To Print ASCII Value Of Digits, Uppercase And
17
4 Lowercase Alphabets
04.10.202
18 Write A Program To Demonstrate The Use Of Class And Object
4
11.10.202
19 Write A Program To Demonstrate The Use Of Constructor In a Class
4
11.10.202 Write A Program To Demonstrate The Use Of Constructor And
20
4 Destructor In A Class
11.10.202
21 Write A Program To Demonstrate The Use Of Static Variable
4
18.10.202
22 Write A Program To Swap Two Numbers Using Class
4
23 18.10.202 Write A Program To Print Numbers From 1 To N Using Class
4
18.10.202
24 Write A Program To Overload A Sum Function
4
25.10.202 Write A Program To Calculate Area Of A Circle, A Rectangle Or A
25
4 Triangle Depending On Input Using Overloaded Calculate Function
25.10.202
26 Write A Program To Print Factorial Of A Given Number Using Class
4
25.10.202
27 Write A Program To Check Whether A Year Is Leap Year Or Not.
4
08.11.202
28 Write A C++ Program To Create A File (Data.Txt)
4
08.11.202
29 Write A Program For Creating And Writing On A Text File
4
08.11.202
30 Write A Program To Retrieve/Read Data From A Text File
4
1.Program to take two numbers and print their sum and average
Code: -
#include <iostream>
using namespace std;
int main() {
double num1, num2, sum, avg;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
sum = num1 + num2;
avg = sum / 2;
cout << "Sum: " << sum << endl;
cout << "Average: " << avg << endl;
return 0;
Output: -
2. Program to swap two numbers without using a third
variable.
Code: -
#include <iostream>
using namespace std;
int main() {
int a, b
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Before swapping: a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping: a = " << a << ", b = " << b << endl;
return 0;
Output: -
3. Program to check whether a number is even or odd.
Code: -
#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;
Output: -
4. Program to find the largest among three numbers.
Code: -
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
int largest = num1;
if (num2 > largest)
largest = num2;
if (num3 > largest)
largest = num3;
cout << "The largest number is: " << largest << endl;
return 0;
Output: -
5. Program to calculate sum, difference, multiplication, and
division.
Code: -
#include <iostream>
using namespace std;
int main() {
double num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Sum: " << num1 + num2 << endl;
cout << "Difference: " << num1 - num2 << endl;
cout << "Multiplication: " << num1 * num2 << endl;
if (num2 != 0)
cout << "Division: " << num1 / num2 << endl;
else
cout << "Division: Undefined (Division by zero)" << endl;
return 0;
Output: -
6. Program to print the table of a number using for loop.
Code: -
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number to print its table: ";
cin >> num;
cout << "Table of " << num << ":" << endl;
for (int i = 1; i <= 10; i++) {
cout << num << " x " << i << " = " << num * i << endl;
return 0;
Output: -
7. Program to print the table of a number using do-while loop.
Code: -
#include <iostream>
using namespace std;
int main() {
int num, i = 1;
cout << "Enter a number to print its table: ";
cin >> num;
cout << "Table of " << num << ":" << endl;
do {
cout << num << " x " << i << " = " << num * i << endl;
i++;
} while (i <= 10);
return 0;
Output: -
8.Write a program to print Fibonacci Series (0, 1, 1, 2, 3, 5, 8,
13, 21...).
Code: -
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of terms for Fibonacci series: ";
cin >> n;
int a = 0, b = 1, c;
cout << "Fibonacci Series: ";
if (n >= 1) cout << a << " ";
if (n >= 2) cout << b << " ";
for (int i = 3; i <= n; i++) {
c = a + b;
cout << c << " ";
a = b;
b = c;
}
cout << endl;
return 0;
Output: -
9. Write a program to reverse a number.
Code: -
#include <iostream>
using namespace std;
int main() {
int num, reverse = 0;
cout << "Enter a number: ";
cin >> num;
while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
cout << "Reversed number: " << reverse << endl;
return 0;
Output: -
10. Write a program to check whether a number is a prime
number or not.
Code: -
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num <= 1) {
cout << num << " is not a prime number." << endl;
return 0;
bool isPrime = true;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = false;
break;
if (isPrime)
cout << num << " is a prime number." << endl;
else
cout << num << " is not a prime number." << endl;
return 0;
Output: -
11. Write a program to convert binary number to decimal
number.
Code: -
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int binary, decimal = 0, i = 0;
cout << "Enter a binary number: ";
cin >> binary;
while (binary != 0) {
int bit = binary % 10;
decimal += bit * pow(2, i);
binary /= 10;
i++;
cout << "Decimal equivalent: " << decimal << endl;
return 0;
Output: -
12. Write a program that takes values in an array and also
display them.
Code: -
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the number of elements: ";
cin >> size;
int arr[size];
cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
cout << "Array elements are: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
cout << endl;
return 0;
Output: -
13. Write a program to print length of a string provided.
Code: -
#include <iostream>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
cout << "Length of the string: " << str.length() << endl;
return 0;
}
Output: -
14. Write a program to check whether a string is palindrome
or not.
Code: -
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
int start = 0;
int end = str.length() - 1;
bool isPalindrome = true;
while (start < end) {
if (str[start] != str[end]) {
isPalindrome = false;
break;
start++;
end--;
if (isPalindrome)
cout << str << " is a palindrome." << endl;
else
cout << str << " is not a palindrome." << endl;
return 0;
}
Output: -
15. Write a program that provides the sum of two matrices.
Code: -
#include <iostream>
using namespace std;
int main() {
int row, col;
cout << "Enter number of rows and columns: ";
cin >> row >> col;
int mat1[row][col], mat2[row][col], sum[row][col];
cout << "Enter elements of first matrix:" << endl;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
cin >> mat1[i][j];
cout << "Enter elements of second matrix:" << endl;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
cin >> mat2[i][j];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
sum[i][j] = mat1[i][j] + mat2[i][j];
cout << "Sum of matrices:" << endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
cout << sum[i][j] << " ";
cout << endl;
}
return 0;
Output: -
16. Write a program to find out the product of two matrices.
Code: -
#include <iostream>
using namespace std;
int main() {
int row1, col1, row2, col2;
cout << "Enter number of rows and columns for first matrix: ";
cin >> row1 >> col1;
cout << "Enter number of rows and columns for second matrix: ";
cin>> row2 >> col2;
if (col1 != row2) {
cout << "Matrix multiplication not possible!" << endl;
return 0;
int mat1[row1][col1], mat2[row2][col2], product[row1][col2];
cout << "Enter elements of first matrix:" << endl;
for (int i = 0; i < row1; i++)
for (int j = 0; j < col1; j++)
cin >> mat1[i][j];
cout << "Enter elements of second matrix:" << endl;
for (int i = 0; i < row2; i++)
for (int j = 0; j < col2; j++)
cin >> mat2[i][j];
for (int i = 0; i < row1; i++)
for (int j = 0; j < col2; j++) {
product[i][j] = 0;
for (int k = 0; k < col1; k++)
product[i][j] += mat1[i][k] * mat2[k][j];
cout << "Product of matrices:" << endl;
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++)
cout << product[i][j] << " ";
cout << endl;
return 0;
}
Output: - -
17. Write a program to print ASCII value of digits, uppercase and
lowercase alphabets.
Code: -
#include <iostream>
using namespace std;
int main() {
cout << "ASCII values of digits:" << endl;
for (char i = '0'; i <= '9'; i++)
cout << i << " = " << int(i) << endl;
cout << "\nASCII values of uppercase letters:" << endl;
for (char i = 'A'; i <= 'Z'; i++)
cout << i << " = " << int(i) << endl;
cout << "\nASCII values of lowercase letters:" << endl;
for (char i = 'a'; i <= 'z'; i++)
cout << i << " = " << int(i) << endl;
return 0;
Output: -
18. Write a program to demonstrate the use of class and object.
Code: -
#include <iostream>
using namespace std;
class Rectangle {
public:
int length, width;
void setDimensions(int l, int w) {
length = l;
width = w;
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
};
int main() {
Rectangle rect;
rect.setDimensions(5, 10);
rect.display();
return 0;
Output: -
19. Write a program to demonstrate the use of constructor in al
class.
Code: -
#include <iostream>
using namespace std;
class Rectangle {
public:
int length, width;
Rectangle(int l, int w) {
length = l;
width = w;
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
};
int main() {
Rectangle rect(5, 10);
rect.display();
return 0;
Output: -
20. Write a program to demonstrate the use of constructor and
destructor in a class.
Code: -
#include <iostream>
using namespace std;
class Rectangle {
public:
int length, width;
Rectangle(int l, int w) {
length = l;
width = w;
cout << "Rectangle created." << endl;
~Rectangle() {
cout << "Rectangle destroyed." << endl;
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
};
int main() {
Rectangle rect(5, 10);
rect.display();
return 0;
Output: -
21. Write a program to demonstrate the use of static variable.
Code: -
#include <iostream>
using namespace std;
class Counter {
public:
static int count;
Counter() {
count++;
}
static void displayCount() {
cout << "Count: " << count << endl;
};
int Counter::count = 0;
int main() {
Counter c1, c2;
Counter::displayCount();
Counter c3;
Counter::displayCount();
return 0;
Output: -
22. Write a program to swap two numbers using class
Code: -
#include <iostream>
using namespace std;
class Swap {
public:
void swapNumbers(int &a, int &b) {
int temp = a;
a = b;
b = temp;
};
int main() {
int a = 5, b = 10;
Swap swapObj;
cout << "Before swapping: a = " << a << ", b = " << b << endl;
swapObj.swapNumbers(a, b);
cout << "After swapping: a = " << a << ", b = " << b << endl;
return 0;
Output: -
23. Write a Program to Print Numbers from 1 to n using class
Code: -
#include <iostream>
using namespace std;
class NumberPrinter {
public:
void printNumbers(int n) {
for (int i = 1; i <= n; i++) {
cout << i << " ";
cout << endl;
};
int main() {
NumberPrinter printer;
int n;
cout << "Enter a number: ";
cin >> n;
printer.printNumbers(n);
return 0;
Output: -
24. Write a program to overload a sum function.
Code: -
#include <iostream>
using namespace std;
class Calculator {
public:
int sum(int a, int b) {
return a + b;
double sum(double a, double b) {
return a + b;
int sum(int a, int b, int c) {
return a + b + c;
};
int main() {
Calculator calc;
cout << "Sum of integers: " << calc.sum(3, 4) << endl;
cout << "Sum of doubles: " << calc.sum(3.5, 4.5) << endl;
cout << "Sum of three integers: " << calc.sum(1, 2, 3) << endl;
return 0;
}
Output: -
25. Write a program to calculate area of a circle, a rectangle or a
triangle depending on input using overloaded calculate function.
Code: -
#include<iostream>
using namespace std;
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
int s,l,b;
float r,bs,ht;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
cout<<"Area of square is"<<area(s);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(bs,ht);
}
int area(int s)
return(s*s);
int area(int l,int b)
return(l*b);
float area(float r)
return(3.14*r*r);
float area(float bs,float ht)
return((bs*ht)/2);
Output: -
26. Write a program to print factorial of a given number using
class.
Code: -
#include <iostream>
using namespace std;
class Factorial {
public:
int calculate(int n) {
if (n == 0 || n == 1) return 1;
return n * calculate(n - 1);
};
int main() {
Factorial fact;
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factorial of " << num << " is: " << fact.calculate(num) << endl;
return 0;
Output: -
27. Write a program to check whether a year is leap year or not.
Code: -
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
cout << year << " is a leap year." << endl;
else
cout << year << " is not a leap year." << endl;
return 0;
Output: -
28. Write a C++ program to create a file (data.txt).
Code: -
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
ofstream outfile("data.txt");
// Check if the file was created successfully
if (outfile.is_open()) {
cout << "File 'data.txt' created successfully." << endl;
outfile.close();
} else {
cout << "Unable to create the file." << endl;
return 0;
}
Output: -
29. Write a program for creating and writing on a text file.
Code: -
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open the file for writing
ofstream outfile("data.txt");
// Check if the file is open
if (outfile.is_open()) {
// Write some content to the file
outfile << "Hello, this is the first line in the file." << endl;
outfile << "This is the second line of the file." << endl;
outfile << "C++ file handling is easy and powerful!" << endl;
cout << "Data written to 'data.txt' successfully." << endl;
outfile.close();
} else {
cout << "Unable to open the file for writing." << endl;
return 0;
Output: -
30. Write a program to retrieve/read data from a text file.
Code: -
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string line;
ifstream infile("data.txt");
if (infile.is_open()) {
cout << "Reading from 'data.txt':" << endl;
while (getline(infile, line)) {
cout << line << endl;
infile.close();
} else {
cout << "Unable to open the file for reading." << endl;
return 0;
Output: -
*-*End*-*