Oops Programs
Oops Programs
h>
#include<conio.h>
#include<math.h>
void main()
float x1,x2,b,a,c,f;
clrscr();
cout<<"Enter a :";
cin>>a;
cout<<"Enter b :";
cin>>b;
cout<<"Enter c :";
cin>>c;
f=abs(sqrt((b*b)-4*a*c));
x1=(-b+f)/2*a;
x2=(-b-f)/2*a;
cout<<"value of x1 : "<<x1<<endl;
cout<<"value of x2 : "<<x2<<endl;
getch();
OUTPUT:
Q.2.Write a C++ program to demonstrate the use of operator precedence.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
float x1,x2;
clrscr();
x1=10/2*3+1-3;
x2=14+6-5*2/2;
getch();
OUTPUT:
Q.1.Write a C++ program check leap year.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
int year;
clrscr();
cin>>year;
if(year%4==0)
else
getch();
OUTPUT:
Q.2.Write a C++ program armstrong numbers between two integers.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
int n1=0,n=0,num=0,digit=0,sum=0;
clrscr();
cin>>num;
n1=num;
while(n1!=0)
n1/=10;
n++;
n1=num;
for(int i=1;i<=n;i++)
digit=n1%10;
sum=sum+digit*digit*digit;
n1=n1/10;
if(sum==num)
else
getch();
OUTPUT:
Q.3.Write a C++ program to check whether a number is palindrome or not.
#include<iostream.h>
#include<conio.h>
void main()
int num=0,re_num=0,number=0,remainder=0;
clrscr();
cin>>num;
number=num;
while(num!=0)
remainder=num%10;
re_num=re_num*10+remainder;
num=num/10;
cout<<re_num<<endl;
if(re_num==number)
cout<<"Number is palindrome";
else
getch();
OUTPUT:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=1;i<=10;i++)
cout<<"7X"<<i<<"="<<7*i<<endl;
getch();
OUTPUT:
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
for(int i=100;i>=1;i--)
{
cout<<i<<"\t";
getch();
OUTPUT:
Q.3.Write a C++ program which will print (half pyramid)pattern of natural numbers
23
456
7 8 9 10
11 12 13 14 15.
#include<iostream.h>
#include<conio.h>
void main()
int i,j,n=1;
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
cout<<n;
n++;
cout<<endl;
getch();
OUTPUT:
Q.4.Write a C++ program; ask the user to enter the number of rows to print pyramid of stars(*)
**
***
*****
*******
#include<iostream.h>
#include<conio.h>
void main()
int i,j,row;
clrscr();
cin>>row;
for(i=1;i<=row;i++)
for(j=0;j<i;j++)
cout<<"*";
cout<<endl;
getch();
OUTPUT:
Q.1.Write a C++ program to find median of two sorted arrays of same size.
#include<iostream.h>
#include<conio.h>
void main()
int a[5]={10,20,30,40,50};
int b[5]={15,35,55,75,95};
int m1,m2,mid1,mid2;
clrscr();
m1=sizeof(a)/sizeof(a[0]);
m2=sizeof(b)/sizeof(b[0]);
mid1=m1/2;
mid2=m1/2;
cout<<"Middle of A :"<<a[mid1]<<endl;
cout<<"Middle of B :"<<b[mid2];
getch();
}
OUTPUT:
Q.2.Write a C++ program to find the two repeating elements in a given array.
#include<iostream.h>
#include<conio.h>
void main()
int a[5]={10,20,40,20,10},i,j;
clrscr();
for(i=0;i<2;i++)
come:
for(j=2;j<5;j++)
if(a[i]==a[j])
if(i==2&&j==2)
goto come;
}
getch();
OUTPUT:
#include<iostream.h>
#include<conio.h>
void main()
int a[5]={1,5,2,4,3};
int temp;
clrscr();
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
if(a[i]<a[j])
temp=a[i];
a[i]=a[j];
a[j]=temp;
for(i=0;i<2;i++)
cout<<i+1<<"number :"<<a[i]<<endl;
getch();
OUTPUT:
Q.4.Write a C++ program to find the missing number.
#include<iostream.h>
#include<conio.h>
void main()
int a[100]={1,2,3,5};
int k=5;
int total=(k)*(k+1)/2;
clrscr();
for(int i=0;i<5;i++)
total=total-a[i];
getch();
OUTPUT:
Q.1.Write a C++ program to Multiply 3*3 Matrix Using Multi-dimensional Arrays.
#include <iostream.h>
#include<conio.h>
void main()
int matrix1[3][3];
int matrix2[3][3];
int result[3][3]={0};
int i,j,k;
clrscr();
for(i=0;i<3;++i)
for(j=0;j<3;++j)
cin>>matrix1[i][j];
for(i=0;i<3;++i)
for(j=0;j<3;++j)
for(i=0;i<3;++i)
for(j=0;j<3;++j)
for(k=0;k<3;++k)
result[i][j]+=matrix1[i][k]*matrix2[k][j];
cout<<"Resultant Matrix:"<<endl;
for(i=0;i<3;++i)
for(j=0;j<3;++j)
cout<<result[i][j]<<" ";
cout<<endl;
getch();
OUTPUT:
Q.2.Write a C++ program to find Transpose of 2*2 matrix.
#include <iostream.h>
#include<conio.h>
void main()
int matrix[2][2];
int transpose[2][2];
int i,j;
clrscr();
for(i=0;i<2;++i)
for(j=0;j<2;++j)
cin>>matrix[i][j];
}
for(i=0;i<2;++i)
for(j=0;j<2;++j)
transpose[j][i]=matrix[i][j];
for(i=0;i<2;++i)
for(j=0;j<2;++j)
cout<<transpose[i][j]<<" ";
cout<<endl;
getch();
OUTPUT:
Q.1.Write a C++ program to declare a class “Book” having data members book_name,author and
price.Accept and display data for book having maximum price.
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
class Book
public:
char book_name[30];
char author[30];
double price;
void read()
cin>>book_name;
cout<<"Price: ";
cin>>price;
void display()
cout<<"Price: "<<price<<"$"<<endl;
double getPrice()
return price;
};
void main()
int num,i=1,j=0,max;
clrscr();
cin>>num;
Book book[100];
for(i=0;i<num;i++)
book[i].read();
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
if(book[i].price>book[j+1].price)
max=i;
break;
book[max].display();
getch();
OUTPUT:
Q.2.Write a C++ program to declare a class “Staff” having data members name,basic salary,DA,HRA
and calculate gross salary.Accept and display data for one staff.
a)Where DA=74.5% of basic
1) HRA=30% of basic
2)Gross_salary=basic+HRA+DA
#include<iostream.h>
#include<conio.h>
class Staff
public:
char name[20];
float HRA,DA,sal,gr_sal;
void accept()
cout<<"Name : ";
cin>>name;
cin>>sal;
void display()
DA=(sal*74.5)/100;
HRA=(sal*30)/100;
gr_sal=sal+HRA+DA;
cout<<"Salary : "<<sal<<endl;
cout<<"HRA : "<<HRA<<endl;
cout<<"DA : "<<DA<<endl;
}
};
void main()
clrscr();
Staff s;
s.accept();
s.display();
getch();
OUTPUT:
PRACTICAL NO.7
#include <iostream.h>
public:
int emp_id;
char emp_name[50];
float emp_salary;
void getData() {
cin.getline(emp_name, 50);
void displayData() {
};
int main() {
clrscr();
int n;
Employee employees[100];
cout << "\nEnter details for Employee " << (i + 1) << ":\n";
employees[i].getData();
employees[i].displayData();
getch();
return 0;
OUTPUT:
#include <iostream.h>
class City {
private:
char name[50];
int population;
public:
void getData() {
cin.getline(name, 50);
void displayData() {
};
int main() {
clrscr();
City cities[10];
cout << "\nEnter details for City " << (i + 1) << ":\n";
cities[i].getData();
cities[i].displayData();
return 0;
OUTPUT:
Write a C++ program to swap the values of two variables using friend functions.
#include <iostream.h>
#include <conio.h>
class Number {
private:
int value;
public:
};
num1.value = num2.value;
num2.value = temp;
int main() {
Number num1(5);
Number num2(10);
display(num1);
display(num2);
swapNumbers(num1, num2);
display(num1);
display(num2);
return 0;
OUTPUT:
Write a C++ program to define a class “Distance” having data member as meters.Display the
addition of two Distances objects using friend function.
#include <iostream.h>
#include <conio.h>
class Distance {
private:
int meters;
public:
Distance(int m) : meters(m) {}
void display() {
cout << "Meters: " << meters << " m" << endl;
}
};
Distance result(totalMeters);
return result;
int main() {
Distance distance1(10);
Distance distance2(20);
distance1.display();
distance2.display();
sum.display();
return 0;
}
OUTPUT:
#include <iostream.h>
#include <conio.h>
class Number {
private:
int x, y;
public:
return x + y;
}
return x - y;
return x * y;
if (y == 0) {
return 0;
return (float)x / y;
};
int main() {
int a, b;
cin >> b;
return 0;
OUTPUT:
#include <iostream.h>
#include <conio.h>
class Number {
private:
int x, y;
public:
~Number() {
int add() {
return x + y;
int subtract() {
return x - y;
int multiply() {
return x * y;
float divide() {
if (y == 0) {
return 0;
return (float)x / y;
};
int main() {
int a, b;
cin >> a;
cin >> b;
return 0;
OUTPUT:
Write a C++ program to define a class “product” having data members prod_id, prod_name and prod_price.
Accept and display data for 3 products using constructor overloading.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Product {
private:
int prod_id;
char prod_name[50];
float prod_price;
public:
// Default constructor
Product() {
prod_id = 0;
prod_name[0] = '\0';
prod_price = 0.0;
prod_id = id;
strcpy(prod_name, name);
prod_price = price;
void acceptData() {
cin.ignore();
cin.getline(prod_name, 50);
void displayData() {
};
int main() {
clrscr();
cout << "Enter details for Product " << i + 1 << ":" << endl;
products[i].acceptData();
cout << "\nProduct " << i + 1 << " details:" << endl;
products[i].displayData();
getch();
return 0;
OUTPUT:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Student {
protected:
int roll_no;
char name[50];
public:
void acceptStudentData() {
cin.ignore();
cin.getline(name, 50);
void displayStudentData() {
};
private:
float total;
float percentage;
public:
void acceptMarksData() {
total = m1 + m2 + m3;
void displayMarksData() {
cout << "Percentage: " << percentage << "%" << endl;
};
void main() {
clrscr();
studentData.acceptStudentData();
studentData.acceptMarksData();
studentData.displayStudentData();
studentData.displayMarksData();
getch();
OUTPUT:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Employee {
protected:
int emp_no;
char emp_name[50];
char emp_designation[50];
public:
void acceptEmployeeData() {
cin.ignore();
cin.getline(emp_name, 50);
cin.getline(emp_designation, 50);
void displayEmployeeData() {
};
private:
public:
void acceptSalaryData() {
void displaySalaryData() {
};
void main() {
clrscr();
employeeData.acceptEmployeeData();
employeeData.acceptSalaryData();
employeeData.displayEmployeeData();
employeeData.displaySalaryData();
getch();
}
OUTPUT:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Person {
protected:
char name[50];
char gender[10];
int age;
public:
void acceptPersonData() {
cin.ignore();
cin.getline(name, 50);
cin.getline(gender, 10);
void displayPersonData() {
};
protected:
int emp_id;
char company[50];
float salary;
public:
void acceptEmployeeData() {
cin.ignore();
cin.getline(company, 50);
void displayEmployeeData() {
};
private:
int no_of_prog_lang_known;
public:
void acceptProgrammerData() {
void displayProgrammerData() {
cout << "Number of Programming Languages Known: " << no_of_prog_lang_known << endl;
};
void main() {
clrscr();
programmerData.acceptPersonData();
programmerData.acceptEmployeeData();
programmerData.acceptProgrammerData();
programmerData.displayPersonData();
programmerData.displayEmployeeData();
getch();
OUTPUT:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Car {
protected:
char car_type[50];
public:
void acceptCarData() {
cin.ignore();
cin.getline(car_type, 50);
void displayCarData() {
};
class Brand {
protected:
char brand_name[50];
int speed;
public:
void acceptBrandData() {
cin.getline(brand_name, 50);
void displayBrandData() {
cout << "Speed: " << speed << " mph" << endl;
};
class Model {
protected:
char model_name[50];
float price;
public:
void acceptModelData() {
cin.ignore();
cin.getline(model_name, 50);
void displayModelData() {
cout << "Model Name: " << model_name << endl;
};
public:
void acceptCarDetails() {
acceptCarData();
acceptBrandData();
acceptModelData();
void displayCarDetails() {
displayCarData();
displayBrandData();
displayModelData();
};
void main() {
clrscr();
carInfo.displayCarDetails();
getch();
OUTPUT:
PRACTICAL NO.13
#include<iostream.h>
#include<conio.h>
class Rectangle {
protected:
float length;
float width;
public:
void acceptDimensions() {
};
class Area {
protected:
float area;
public:
area = l * w;
void displayArea() {
cout << "Area of the rectangle: " << area << endl;
};
class Perimeter {
protected:
float perimeter;
public:
perimeter = 2 * (l + w);
void displayPerimeter() {
cout << "Perimeter of the rectangle: " << perimeter << endl;
};
public:
void calculateAndDisplay() {
calculateArea(length, width);
displayArea();
calculatePerimeter(length, width);
displayPerimeter();
};
void main() {
clrscr();
rectInfo.acceptDimensions();
rectInfo.calculateAndDisplay();
getch();
OUTPUT:
Write a C++ program for presentation of class hierarchy as below assume suitable data and function
members.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Cricketer {
protected:
char name[50];
int age;
public:
void displayDetails() {
cout << "Age: " << age << " years" << endl;
};
class Bowler : public Cricketer {
protected:
int wickets;
public:
Bowler(const char* _name, int _age, int _wickets) : Cricketer(_name, _age), wickets(_wickets) {}
void displayBowlerDetails() {
};
protected:
int runsScored;
public:
void displayBatsmanDetails() {
};
public:
AllRounder(const char* _name, int _age, int _wickets, int _runsScored)
void displayAllRounderDetails() {
};
void main() {
clrscr();
bowler.displayBowlerDetails();
batsman.displayBatsmanDetails();
allRounder.displayAllRounderDetails();
getch();
OUTPUT:
PRACTICAL NO.14
Write a C++ declare a class members
#include <iostream.h>
#include <conio.h>
class Book {
private:
char book_name[50];
char author_name[50];
float price;
public:
void input() {
cin.getline(book_name, 50);
cout << "Enter Author Name: ";
cin.getline(author_name, 50);
void display() {
};
int main() {
clrscr();
bookPtr->input();
bookPtr->display();
delete bookPtr; // Free the memory
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <conio.h>
class Box {
private:
float height;
float width;
float breadth;
public:
void input() {
void display() {
float calculateVolume() {
float calculateArea() {
}
};
int main() {
clrscr();
boxPtr->input();
boxPtr->display();
getch();
return 0;
}
OUTPUT:
#include <iostream.h>
#include <conio.h>
class Birthday {
private:
int day;
int month;
int year;
public:
void input() {
void display() {
};
int main() {
clrscr();
birthdayArray[i].input();
birthdayArray[i].display();
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <conio.h>
class Polygon {
protected:
float width;
float height;
public:
return 0.0;
};
class Rectangle : public Polygon {
public:
float area() {
};
public:
float area() {
};
int main() {
clrscr();
delete rectPtr;
delete triPtr;
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <conio.h>
class Counter {
private:
int count;
public:
Counter() : count(0) {}
void display() {
Counter& operator++() {
count++;
return *this;
Counter& operator--() {
count--;
return *this;
Counter operator++(int) {
count++;
return temp;
Counter operator--(int) {
count--;
return temp;
};
int main() {
clrscr();
Counter c;
c.display();
c++;
c.display();
c--;
c.display();
++c;
c.display();
--c;
c.display();
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <conio.h>
class Complex {
private:
float real;
float imag;
public:
void display() {
if (imag >= 0)
else
};
int main() {
clrscr();
Complex num1(3.0, 4.0); // 3 + 4i
num1.display();
num2.display();
sum.display();
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <conio.h>
class Binary {
private:
float value;
public:
Binary() : value(0.0) {}
Binary(float v) : value(v) {}
void display() {
if (other.value != 0.0)
else {
return Binary();
};
int main() {
clrscr();
Binary num1(5.0);
Binary num2(3.0);
num1.display();
num2.display();
result1.display();
result2.display();
result3.display();
result4.display();
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <conio.h>
#include <string.h>
class StringCompare {
private:
char* str;
public:
StringCompare(const char* s) {
strcpy(str, s);
}
~StringCompare() {
delete[] str;
};
int main() {
clrscr();
StringCompare str1("Hello");
StringCompare str2("World");
StringCompare str3("Hello");
if (str1 == str2) {
} else {
if (str1 == str3) {
cout << "str1 is equal to str3" << endl;
} else {
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <conio.h>
int temp = a;
a = b;
b = temp;
float temp = a;
a = b;
b = temp;
char temp = a;
a = b;
b = temp;
int main() {
clrscr();
cout << "int1: " << int1 << " int2: " << int2 << endl;
cout << "float1: " << float1 << " float2: " << float2 << endl;
cout << "char1: " << char1 << " char2: " << char2 << endl;
swap(int1, int2);
swap(float1, float2);
swap(char1, char2);
cout << "int1: " << int1 << " int2: " << int2 << endl;
cout << "float1: " << float1 << " float2: " << float2 << endl;
cout << "char1: " << char1 << " char2: " << char2 << endl;
getch();
return 0;
OUTPUT:
Write a C++ program that find the distance between two points in 2d and 3d space using function
overloading
#include <iostream.h>
#include <conio.h>
#include <math.h>
class Point2D {
private:
float x;
float y;
public:
float dx = x - other.x;
float dy = y - other.y;
};
class Point3D {
private:
float x;
float y;
float z;
public:
float dx = x - other.x;
float dy = y - other.y;
float dz = z - other.z;
};
int main() {
clrscr();
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <conio.h>
#include <math.h>
class AreaCalculator {
public:
// Calculate the area of a square
float calculateSquareArea(float side) {
return side * side;
}
int main() {
clrscr();
AreaCalculator calculator;
// Square
float squareArea = calculator.calculateSquareArea(5.0);
cout << "Area of Square: " << squareArea << endl;
// Rectangle
float rectangleArea = calculator.calculateRectangleArea(6.0, 4.0);
cout << "Area of Rectangle: " << rectangleArea << endl;
// Circle
float circleArea = calculator.calculateCircleArea(3.0);
cout << "Area of Circle: " << circleArea << endl;
// Triangle
float triangleArea = calculator.calculateTriangleArea(4.0, 3.0);
cout << "Area of Triangle: " << triangleArea << endl;
// Trapezoid
float trapezoidArea = calculator.calculateTrapezoidArea(2.0, 6.0, 4.0);
cout << "Area of Trapezoid: " << trapezoidArea << endl;
getch();
return 0;
}
OUTPUT:
Write a C++ program ask to the user to enter file name to encrypt its content.
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
ifstream inputFile(filename);
if (!inputFile) {
cout << "Unable to open the file: " << filename << endl;
return;
}
ofstream outputFile("encrypted.txt");
if (!outputFile) {
return;
char ch;
while (inputFile.get(ch)) {
if (isalpha(ch)) {
outputFile.put(ch);
inputFile.close();
outputFile.close();
cout << "File content encrypted and saved to 'encrypted.txt'" << endl;
int main() {
clrscr();
char filename[100];
cin.getline(filename, sizeof(filename));
encryptFileContent(filename);
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
void mergeFiles(const char* file1, const char* file2, const char* outputFile) {
ifstream inputFile1(file1);
ifstream inputFile2(file2);
ofstream outputFileStream(outputFile);
return;
char ch;
while (inputFile1.get(ch)) {
outputFileStream.put(ch);
while (inputFile2.get(ch)) {
outputFileStream.put(ch);
}
inputFile1.close();
inputFile2.close();
outputFileStream.close();
cout << "Files merged successfully. Merged content is saved in '" << outputFile << "'"
<< endl;
int main() {
clrscr();
char file1[100];
char file2[100];
char outputFile[100];
cin.getline(file1, sizeof(file1));
cin.getline(file2, sizeof(file2));
cin.getline(outputFile, sizeof(outputFile));
mergeFiles(file1, file2, outputFile);
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
ifstream file(filename);
if (!file) {
cout << "Unable to open the file: " << filename << endl;
return;
char ch;
while (file.get(ch)) {
file.close();
int main() {
clrscr();
char filename[100];
cin.getline(filename, sizeof(filename));
displayFileContents(filename);
getch();
return 0;
OUTPUT:
#include <iostream.h>
#include <conio.h>
#include <dir.h>
int main() {
clrscr();
if (result == 0) {
do {
getch();
return 0;
OUTPUT: