You are on page 1of 26

1. Write a Program to reverse a string.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void reverse(char[]);
void main()
{
clrscr();
char str[20];
cout<<"Enter string"<<endl ;
cin.getline(str,20);
cout<<"\n\n The original string is"<<str<<endl;
reverse(str);
cout<<"\n\n the reverse string is"<<str;
getch();
}
void reverse(char xt[])
{
int l;
int len=strlen(xt);
for(int i=0;i<len/2;i++)
{
char t;
t=xt[i];
xt[i]=xt[len-1-i];
xt[len-1-i]=t;
}
}




//Output
Enter String Pace College
The original string is Pace College
The reverse string is egelloC ecaP
































2. Write a Program to search a number using binary search

#include<iostream.h>
#include<conio.h>
int bsearch(int[],int);
void main()
{
int a[10],i,d,loc;
clrscr();
cout<<"enter any 10 numbers in ascending order";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"enter the number to be searched";
cin>>d;
loc=bsearch(a,d);
if(loc)
cout<<"\nNumber found at location:"<<loc;
else
cout<<"\nNumber not found";
getch();
}
int bsearch(int y[],int dt)
{
int mid,lb,ub;
lb=0; ub=10-1;
while(ub>=lb)
{
mid=(ub+lb)/2;
if(y[mid]==dt)
return mid+1;
if(y[mid<dt)
lb=mid+1;
else
ub=mid-1;
}
return 0;
}






























Output

Enter any 10 numbers 1 3 4 5 6 8 9 12 14 15
Enter to be searched 8
Number found at location 6
Enter any 10 numbers 1 3 4 5 6 8 9 12 14 15
Enter to be searched 18
Number not found


















3. Write a Program to sort numbers to Bubble Sort
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,j,temp;
cout<<"Enter the elements of the array\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"Original array is";
for(i=0;i<10;i++)
{
cout<<a[i];
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"After Bubble sort Ascending order is\n";
for(i=0;i<10;i++)
{
cout<<a[i];
}
getch();
}

Output
Enter the elements of the array
10
9
8
7
6
5
4
3
2
1
Original Array is
10
9
8
7
6
5
4
3
2
1
After Bubble sort Ascending order is
1
2
3
4
5
6
7
8
9
10

4. Write a Program to Swap two numbers using Call by Reference
#include<iostream.h>
#include<conio.h>
void swap(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void main()
{ clrscr();
int x,y;
cout<<"Enter two numbers\n";
cin>>x>>y;
cout<<"Original value\n";
cout<<x<<"\t"<<y;
swap(x,y);
cout<<"\n After Swapping\n";
cout<<x<<"\t"<<y;
getch();
}











Output
Enter two numbers
10 20
Original Value
10 20
After Swapping
20 10


















5. Write a Program to calculate area and circumference of circle using class
#include<iostream.h>
#include<conio.h>
class circle
{
private: float r,a,c,x,y;
public: circle()
{ r=7; x=6; y=8; }
void area()
{ a=3.14*r*r; }
void circum()
{ c=2*3.14*r; }
void print()
{
cout<<"The radius of the circle is"<<r;
cout<<"\n\nThe x,y coordinate of center is"<<x<<","<<y;
cout<<"\n\nThe area of circle is="<<a;
cout<<"\n\nThe circumference of circle is"<<c;
}
};
void main()
{ clrscr();
circle c;
c.area();
c.circum();
c.print();
}








Output
The radius of the circle is 7
The x,y, coordinate of center is 6,8
The area of circle is 153.860001
The circumference of circle is 43.959999



















6. Write a Program using class inheritance

#include<iostream.h>
#include<conio.h>
class student
{
protected: int rollno;
public: void setno(int a)
{
roll no=a;
}
void dispro()
{
cout<<"Roll no="<<rollno<<endl;
}
};
class test: public student
{
protected: float marks1,marks2;
public: void setmarks(float x,float y)
{
marks1=x; marks2=y;
}
void dismarks()
{
cout<<"Marks obtained in 1st test="<<marks1<<endl;
cout<<"Marks obtained in 2nd test="<<marks2<<endl;
}
};
class sports
{
protected: float score;

public: void setscore(float x)
{
score=x;
}
void dispscore()
{
cout<<"\nScore in sports="<<score<<endl;
}
};
class result: public test, public sports
{
float total;
public: void display()
{
total=marks1+marks2+score;
dispro();
dispmarks();
dispscore();
cout<<"Total marks"<<total<<endl;
}
};
void main()
{
clrscr();
result s1;
s1.setno(100);
s1.setmarks(20,20);
s1.setscore(10);
s1.display();
getch();
}




7. Write a Program using Constructor and Destructor

#include<iostream.h>
#include<conio.h>
class ratio
{
public: ratio()
{
cout<<"\n\nThe Object is born";
}
void f1()
{
cout<<"\n\nNow object is alive";
}
~ratio()
{
cout<<"\n\nObject dies";
}
};
void main()
{
clrscr();
ratio r;
r.f1();
getch();
}









Output

The object is Born

Now object is alive

Object dies


















8. Write a Program polar-rectangle & rectangle-polar conversion
#include<iostream.h>
#include<conio.h>
#include<math.h>
class rectangle
{
float x,y;
public:void getdata1()
{
cout<<"Enter the rectangular coordinates";
cout<<"\nx=";
cin>>x;
cout<<"\ny=";
cin>>y;
}
void con_rec_pol()
{
float r,t;
r=sqrt(x*x+y*y);
t=atan(y/x)*180/3.14;
cout<<"\nRectangle to polar conversion is";
cout<<"\n r="<<r;
cout<<"\n theta="<<t;
}
};
class polar
{
float r,t;
public:void getdata2()
{
cout<<"\n\nEnter the polar coordinates";
cout<<"\nr=";
cin>>r;
cout<<"\ntheta=";
cin>>t;
}
void conv_pol_rec()
{
float x,y;
x=r*cos(t*3.14/180);
y=r*sin(t*3.14/180);
cout<<"\n\nPolar to Rectangle conversion is";
cout<<"\n x="<<x;
cout<<"\n y="<<y;
}
};
void main()
{
clrscr();
rectangle rect1;
rect1.getdata1();
rect1.con_rec_pol();
polar pol1;
pol1.getdata2();
pol1.conv_pol_rec();
getch();
}













Output

Enter the rectangular coordinates
x=2
y=3
Rectangle to polar conversion is
r=3.605551
theta=56.338493
Enter the polar coordinates
r=3.605551
theta=56.338493
Polar to rectangle conversion is
x=2
y=3





















9. Write a Program with a Ratio class using Member Function

#include<iostream.h>
#include<conio.h>
class ratio
{
int num,den;
public:void assign();
double convert();
void invert();
void print();
};
void ratio::assign()
{
cout<<"Enter numerator"<<endl;
cin>>num;
cout<<"Enter denominator"<<endl;
cin>>den;
}
double ratio::convert()
{
return (num/den);
}
void ratio::invert()
{
int temp;
temp=num;
num=den;
den=temp;
}
void ratio::print()
{
cout<<num<<"/"<<den<<endl;
}
void main()
{ clrscr();
ratio r;
r.assign();
cout<<"Original ratio is=";
r.print();
cout<<"Inverse ratio is=";
r.convert();
r.invert();
r.print();
getch();
}
























Output
Enter Numerator
2
Enter Denominator
3
Original Value is = 2/3
Inverse Value is = 3/2



10. Write a Program using virtual function
#include<string.h>
#include<iostream.h>
#include<conio.h>
class Person
{
private: char name[80];
public: Person()
{
strcpy(name,"BOB");
}
virtual void print()
{
cout<<"\nName of the person assigned through base object is"<<name;
}
};
class student:public Person
{
private: char name1[80];
public: student()
{
strcpy(name1,"TOM");
}
void print()
{
cout<<"\nName of the person assigned through derived object"<<name1;
}
};
void main()
{
clrscr();
Person *p,x;
student y;
p=&x;
p->print();
p=&y;
p->print();
getch();
}





















Output

Name of the person assigned through base object is BOB
Name of the person assigned through derived object is TOM































11. Write a Program using files in C++


#include<iostream.h>
#include<conio.h>
#include<fstream.h>

void main()
{
clrscr();
char cname[80],cap[80];
ofstream outf1,outf2;
outf1.open("country.txt");
outf1<<"India\n";
outf1<<"China\n";
outf1<<"USA\n";
outf1<<"England\n";
outf1<<"France\n";
outf1.close();

outf2.open("capital.txt");
outf2<<"New Delhi\n";
outf2<<"Beijing\n";
outf2<<"Washington\n";
outf2<<"London\n";
outf2<<"Paris";
outf2.close();

ifstream inf1,inf2;
inf1.open("country.txt");
inf2.open("capital.txt");
while(!inf1.eof() && !inf2.eof())
{
inf1.getline(cname,80);
inf2.getline(cap,80);
cout<<"\nCapital of"<<cname<<"is"<<cap<<"\n";
}
inf1.close();
inf2.close();
getch();
}





























Output

Capital of India is New Delhi

Capital of China is Beijing

Capital of USA is Washington

Capital of England is London

Capital of France is Paris