OOPS File
OOPS File
#include<conio.h>
void main()
clrscr();
int n;
cin >> n;
cout << "Factorial of " << n << " = " << factorial(n);
getch();
int factorial(int n) {
if(n > 1)
else
return 1;
}
Program-2
#include<conio.h>
int Fibonacci(int n)
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
void main() {
clrscr();
int n,i=0,c;
scanf("%d",&n);
printf("\nFibonacci series\n");
printf("%d\t", Fibonacci(i));
i++;
getch();
}
Program-3
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
clrscr();
int r1,c1,r2,c2,i,j,k,sum;
cin>>r1;
cin>>c1;
cin>>r2;
cin>>c2;
if(c1!=r2)
getch();
exit(0);
}
cout<<"\nEnter element of 1st matrix :";
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
cin>>a[i][j];
cout<<" ";
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
cin>>b[i][j];
cout<<" ";
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
int sum=0;
for(int k=0;k<c1;k++)
sum=sum+a[i][k]*b[k][j];
s[i][j]=sum;
}
for(i=0;i<r1;i++)
cout<<"\n";
for(j=0;j<c2;j++)
cout<<s[i][j];
cout<<"\t";
getch();
}
Program-4
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int num,temp,i,flag=0;
cin>>num;
if(num==1||num==2)
for(i=2;i<=num/2;i++)
if(num%i==0)
flag=1;
break;
}
if(flag==0)
getch();
Program-5
Write a program to check whether given number is palindrome or
not
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int num,temp,i,rev=0,pop;
cin>>num;
temp=num;
while(temp>0)
pop=temp%10;
rev=(rev*10)+pop;
temp=temp/10;
if(rev==num)
else
getch();
Program-7
WAP of the class cylinder having the following : Data types - length, height,
pi=3.14Functions – calculate volume(), surface area (), area of cylinder()
Using scope resolution operator
#include<iostream.h>
#include<conio.h>
class cylinder
private:
float r;
float h;
float pi;
public:
cylinder() : pi(3.14) {}
float volume(float,float);
float area(float,float);
float sur_area(float,float);
};
r=x;
h=y;
return(pi*r*r*h);
r=x;
h=y;
return(2*pi*r*r+2*pi*r*h);
r=x;
h=y;
return(2*pi*r*h);
cylinder c1;
void main()
clrscr();
int radius,height;
cin>>radius;
cin>>height;
getch();
Program-7
WAP of class student & display the data of a student of a class having 30
students. Write a class student having the following -
Functions: readdata(),displaydata(),computedata().
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
int rollno;
char name[30];
float marks[5];
float total,percentage;
public:
void readdata();
void displaydata();
void computedata();
};
void student::readdata()
cin>>rollno;
cin>>name;
}
void student::computedata()
float total=0;
for(int i=0;i<5;i++)
cin>>marks[i];
total=total+marks[i];
percentage=total/5;
void student::displaydata()
cout<<"\nRoll no : "<<rollno<<endl;
cout<<"Name : "<<name<<endl;
for(int i=0;i<5;i++)
cout<<"Percentage : "<<percentage<<endl;
int main()
student s[30];
for(int i=0;i<30;i++)
{
clrscr();
s[i].readdata();
s[i].computedata();
getch();
clrscr();
for(int j=0;j<30;j++)
s[j].displaydata();
getch();
return 0;
}
Program -8
A) Pass by Value
#include<iostream.h>
#include<conio.h>
class Account
char name[20];
float balance;
public:
void getdata()
cin>>accno;
cin>>name;
cin>>balance;
void display()
{
cout<<"\nAcoount Number: "<<accno;
a.balance=a.balance+amount;
};
int main()
clrscr();
Account a1,a2,a3;
float amo;
cout<<"\t\t\t\tPass by value";
a1.getdata();
cin>>amo;
a1.transfer(a1,amo);
a1.display();
getch();
return 0;
}
B) Pass by reference
#include<iostream.h>
#include<conio.h>
class Account
char name[20];
float balance;
public:
void getdata()
cin>>accno;
cin>>name;
cin>>balance;
void display()
a.balance=a.balance+amount;
};
int main()
clrscr();
Account a1,a2,a3;
float amo;
cout<<"\t\t\t\tPass by Reference";
a1.getdata();
cin>>amo;
a1.transfer(a1,amo);
a1.display();
getch();
return 0;
}
C) Pass by address
#include<iostream.h>
#include<conio.h>
class Account
char name[20];
float balance;
public:
void getdata()
cin>>accno;
cin>>name;
cin>>balance;
void display()
}
void transfer(Account *a,float amount)
a->balance=a->balance+amount;
};
int main()
clrscr();
Account a1,a2,a3;
float amo;
cout<<"\t\t\t\tPass by value";
a1.getdata();
cin>>amo;
a1.transfer(&a1,amo);
a1.display();
getch();
return 0;
}
Program-9
WAP for addition of two complex numbers using returning object from
function
#include<iostream.h>
#include<conio.h>
class complex
int real;
int img;
public:
void getdata()
cin>>real;
cin>>img;
void display()
cout<<real<<" + "<<img<<"i";
};
complex complex::add(complex c)
{
complex temp;
temp.real=real+c.real;
temp.img=img+c.img;
return temp;
int main()
clrscr();
complex c1,c2,c3;
c1.getdata();
c2.getdata();
c3=c1.add(c2);
c3.display();
getch();
return 0;
}
Program – 10
#include<iostream.h>
#include<conio.h>
class account
int salary;
public:
void getdata()
cin>>salary;
void show()
};
int addbonus(account a)
return (a.salary+5000);
int main()
{
clrscr();
account a;
a.getdata();
a.show();
getch();
return 0;
}
Program-11
#include<iostream.h>
#include<conio.h>
class ABC
int num;
public:
ABC()
num=20;
};
class xyz
int value;
public:
xyz()
value=30;
int add()
{
clrscr();
ABC a;
return a.num+value;
};
int main()
xyz obj;
getch();
return 0;
}
Program – 12
#include<iostream.h>
#include<conio.h>
class number
int num;
public:
number()
cin>>num;
return (num*num);
};
int main()
clrscr();
number n;
int val=n.square();
getch();
return 0;
}
Program – 13
#include<iostream.h>
#include<conio.h>
float area(float s)
return (s*s);
return (l*b);
return (0.5*h*b);
int main()
clrscr();
getch();
return 0;
}
Program – 14
#include<iostream.h>
#include<conio.h>
class student
int rollno;
char name[15];
float *marks;
public:
student()
cin>>rollno;
cin>>name;
marks=new float;
cin>>*marks;
void display()
cout<<"\n\nRoll no : "<<rollno;
cout<<"\nName : "<<name;
};
int main()
clrscr();
student s1;
s1.display();
getch();
return 0;
}
Program – 15
#include<iostream.h>
#include<conio.h>
class fraction
float num;
float den;
public:
void get()
cin>>num>>den;
void show()
};
fraction temp;
temp.num=a.num*b.den;
temp.den=a.den*b.num;
return temp;
int main()
clrscr();
fraction f1,f2,f3;
f1.get();
f2.get();
f3=f1/f2;
f3.show();
getch();
return 0;
}
Program – 16
#include<iostream.h>
#include<conio.h>
class length
public:
int l;
void len()
cin>>l;
};
public:
int b;
void bre()
cin>>b;
};
class height
{
public:
int h;
void hei()
cin>>h;
};
public:
int res;
void value()
len();
bre();
hei();
res=l*b*h;
cout<<"\n\nResult : "<<res;
};
int main()
clrscr();
result r1;
r1.value();
getch();
return 0;
}
Program – 17
WAP to count the number of object created in the program using a static
counter
#include<iostream.h>
#include<conio.h>
class myclass
public:
myclass()
created ++;
alive ++;
~myclass()
alive--;
};
int myclass:: created =0;
int myclass::alive=0;
int main()
clrscr();
myclass a1;
myclass a2,a3;
myclass a4,a5;
myclass a6,a7;
myclass a8;
getch();
return 0;
}
Program – 18
#include<iostream.h>
#include<conio.h>
class marks
int internal;
public:
marks()
cin>>internal;
};
int ext;
public:
external()
void show()
marks::show();
};
int main()
clrscr();
marks *p;
external e;
p=&e;
p->show();
getch();
return 0;
}
Program – 19
int main()
try
int no = 10;
int d;
cin>>d;
int res;
if(d != 0)
res = no/d;
cout<<res;
else
throw(d);
catch (int n)
cout<<"Exception Occured";
return 0;
}
Program – 20
#include<iostream.h>
#include<conio.h>
t temp;
temp=x;
x=y;
y=temp;
int main()
clrscr();
int a,b;
double num1,num2;
char str1,str2;
cin>>a;
cout<<"\nEnter value of b : ";
cin>>b;
cin>>num1;
cin>>num2;
cin>>str1;
cin>>str2;
getch();
clrscr();
cout<<"\nValue of a : "<<a;
cout<<"\nValue of b : "<<b;
swap(a,b);
swap(num1,num2);
swap(str1,str2);
cout<<"\nValue of a : "<<a;
cout<<"\nValue of b : "<<b;
cout<<"\nValue of double num1 : "<<num1;
getch();
return 0;