1-12(Lab Programs)
1-12(Lab Programs)
#include<iostream.h>
#include<conio.h>
class frequency
private:
int a[100],n,ele,freq;
public:
void getdata();
void compute();
void display();
};
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
cin>>ele;
freq=0;
for(int i=0;i<n;i++)
if(a[i]==ele)
Page |2
freq++;
if(freq>0)
else
void main()
frequency f;
clrscr();
f.getdata();
f.compute();
f.display();
getch();
Output 1
Output2
Page |3
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
class insert
private:
int a[100],ele,pos,n;
public:
void getdata();
void compute();
void display();
};
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
cin>>ele;
cin>>pos;
}
Page |4
if(pos>n)
cout<<"Invalid position"<<endl;
getch();
exit(0);
for(int i=n-1;i>=pos;i--)
a[i+1]=a[i];
a[pos]=ele;
n++;
for(int i=0;i<n;i++)
cout<<a[i]<<setw(4);
void main()
insert i;
clrscr();
i.getdata();
Page |5
i.compute();
i.display();
getch();
Output 1
Output 2
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
class deletion
private:
int a[100],pos,n,ele;
public:
void getdata();
void compute();
Page |6
void display();
};
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
cin>>pos;
void deletion::compute()
if(pos>n-1)
cout<<"Invalid position"<<endl;
getch();
exit(0);
ele=a[pos];
for(int i=pos+1;i<n;i++)
a[i-1]=a[i];
n--;
void deletion::display()
Page |7
for(int i=0;i<n;i++)
cout<<a[i]<<setw(4);
void main()
deletion d;
clrscr();
d.getdata();
d.compute();
d.display();
getch();
Output 1
Output 2
Page |8
{
cout<<a[i]<<setw(4);
}
}
void main()
{
clrscr();
sorting s;
s.getdata();
s.compute();
s.display();
getch();
}
Output
{
int low,high,mid;
low=0;
high=n-1;
loc=-1;
while(low<=high)
{
mid=(low+high)/2;
if(ele==a[mid])
{
loc=mid;
break;
}
else if(ele<a[mid])
{
high=mid-1;
}
else
{
low=mid+1;
}
}
}
void Binarysearch::display()
{
if(loc>=0)
{
cout<<"Location of the element in array is "<<loc;
}
else
{
cout<<"Sorry Buddy,Element doesnt exist"<<endl;
}
}
void main()
{
clrscr();
Binarysearch b;
b.getdata();
b.compute();
b.display();
getch();
}
P a g e | 11
Output 1
Output 2
6. Write a program to create a class with data members principle, time and rate.
Create member functions to accept data values to compute simple interest
and to display the result.
#include<iostream.h>
#include<conio.h>
class Simpleinterest
{
private:
double p,t,r,si;
public:
void getdata();
void compute();
void display();
};
void Simpleinterest:: getdata()
{
cout<<"Enter the value of Principal,Time and Rate"<<endl;
cin>>p>>t>>r;
}
void Simpleinterest:: compute()
{
si=(p*t*r)/100;
}
void Simpleinterest::display()
{
cout<<"Principle="<<p<<endl;
cout<<"Time="<<t<<endl;
cout<<"Rate="<<r<<endl;
cout<<"Simple Interest="<<si<<endl;
}
void main()
{
Simpleinterest s;
clrscr();
P a g e | 12
s.getdata();
s.compute();
s.display();
getch();
}
Output 1
7. Write a program to create a class with data members a, b, c and member functions
to input data, compute the discriminant based on the following conditions and
print the roots.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
class Quadratic
private:
double a,b,c,r1,r2;
public:
void getdata();
void compute();
void display();
};
{
P a g e | 13
cin>>a>>b>>c;
void Quadratic::compute()
double d;
d=(b*b)-(4*a*c);
if(d==0)
r1=(-b)/(2*a);
r2=r1;
else if(d>0)
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
else
getch();
exit(0);
void Quadratic::display()
}
P a g e | 14
void main()
clrscr();
Quadratic q;
q.getdata();
q.compute();
q.display();
getch();
Output 1
Output 2
Output 3
#include<conio.h>
#include<math.h>
class functionoverloading
private:
P a g e | 15
double s;
public:
double area(double a)
return a*a;
return l*b;
s=(a+b+c)/2;
};
void main()
clrscr();
functionoverloading f;
double x,y,z,choice;
cin>>choice;
if (choice==1)
cin>>x;
else if(choice==2)
cin>>x>>y;
else if(choice==3)
cin>>x>>y>>z;
else
cout<<"Invalid input"<<endl;
getch();
Output 1
Output 2
P a g e | 17
Output 3
Output 4
Output 2
10. Write a program to find the sum of the series 1+ x + x2 + ... + xn using constructors.
P a g e | 18
#include<iostream.h>
#include<conio.h>
#include<math.h>
class sumofseries
private:
int sum,power,base;
public:
sumofseries(int a,int b)
base=a;
power=b;
int compute();
};
int sumofseries::compute()
sum=1;
for(int i=1;i<=power;i++)
sum=sum+pow(base,i);
return sum;
void main()
clrscr();
int x,y;
P a g e | 19
cin>>x>>y;
sumofseries obj1(x,y);
sumofseries obj2=obj1;
getch();
Output
11. Create a base class containing the data members roll number and name.
Also create a member function to read and display the data using the
concept of single level inheritance. Create a derived class that contains marks of
two subjects and total marks as the data members.
#include<iostream.h>
#include<conio.h>
class student
{
private:
char name[20];
int regno;
public:
void getdata1()
{
cout<<"Enter your name"<<endl;
cin>>name;
cout<<"Enter your Register Number"<<endl;
cin>>regno;
}
void putdata1()
{
cout<<"Name="<<name<<endl;
cout<<"Register Number="<<regno<<endl;
}
};
class marks:public student
{
private:
P a g e | 20
double m1,m2,total;
public:
void getdata2()
{
cout<<"Enter the marks of subject1 and subject2"<<endl;
cin>>m1>>m2;
total=m1+m2;
}
void putdata2()
{
cout<<"Subject1 mark="<<m1<<endl;
cout<<"Subject2 mark="<<m2<<endl;
cout<<"Total Marks="<<total<<endl;
}
};
void main()
{
clrscr();
marks m;
m.getdata1();
m.getdata2();
m.putdata1();
m.putdata2();
getch();
}
Output
12. Create a class containing the following data members register No., name
and fees. Also create a member function to read and display the data using
the concept of pointers to objects.
#include<iostream.h>
#include<conio.h>
class pointers
P a g e | 21
{
private:
char name[20];
int regno;
double fees;
public:
void getdata();
void display();
};
void pointers::getdata()
{
cout<<"Enter your name"<<endl;
cin>>name;
cout<<"Enter your Register Number"<<endl;
cin>>regno;
cout<<"Enter the fees amount"<<endl;
cin>>fees;
}
void pointers::display()
{
cout<<"Name="<<name<<endl;
cout<<"Register Number="<<regno<<endl;
cout<<"Fees="<<fees<<endl;
}
void main()
{
clrscr();
pointers p,*sp;
sp=&p;
sp->getdata();
sp->display();
getch();
}
Output