All - CPP - Programs
All - CPP - Programs
Program:-
# include<iostream.h>
# include<conio.h>
void main()
{
clrscr();
float DATA[100],temp;
int i,k,N,ptr;
cout<<"Enter the dimension of array : ";
cin>>N;
cout<<"Enter the elements of array :\n";
for(i=1;i<=N;i++)
cin>>DATA[i];
cout<<"Original array is ";
for(i=1;i<=N;i++)
{
cout<<endl<<"DATA["<<i<<"] = "<<DATA[i];
}
// LOGIC FOR BUBBLE SORT
for(k=1;k<=N-1;k++)
{
ptr=1;
while(ptr<=N-k)
{
if(DATA[ptr]>DATA[ptr+1])
{
temp=DATA[ptr];
DATA[ptr]=DATA[ptr+1];
DATA[ptr+1]=temp;
}
ptr=ptr+1;
}
}
cout<<endl<<"----------------------------------------
-----";
cout<<endl<<"Sorted array is ";
for(i=1;i<=N;i++)
cout<<endl<<"DATA["<<i<<"] = "<<DATA[i];
getch();
}
Output:-
Enter the dimension of array : 5
Enter the elements of array :
22
44
11
33
55
Original array is
DATA[1] = 22
DATA[2] = 44
DATA[3] = 11
DATA[4] = 33
DATA[5] = 55
---------------------------------------------
Sorted array is
DATA[1] = 11
DATA[2] = 22
DATA[3] = 33
DATA[4] = 44
DATA[5] = 55
Program:-
# include<iostream.h>
# include<conio.h>
void swap(int &x,int &y);
void main()
{
clrscr();
int a=10,b=20;
cout<<endl<<"FROM MAIN FUNCTION";
cout<<endl<<"A = "<<a;
cout<<endl<<"B = "<<b;
swap(a,b);
cout<<endl<<"FROM MAIN FUNCTION";
cout<<endl<<"A = "<<a;
cout<<endl<<"B = "<<b;
getch();
}
void swap(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
cout<<endl<<"FROM SWAP FUNCTION";
cout<<endl<<"A = "<<x;
cout<<endl<<"B = "<<y;
}
Output:-
Program:-
# include<iostream.h>
# include<conio.h>
void main()
{
clrscr();
float DATA[100],ITEM;
int N,LB,UB,MID;
cout<<"Enter the dimension of array : ";
cin>>N;
cout<<"Enter the numbers of array : \n";
for(int i=1;i<=N;i++)
{
cin>>DATA[i];
}
// LOGIC FOR SIMPLE SORT
for(i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
if(DATA[i]<DATA[j])
{
float temp=DATA[i];
DATA[i]=DATA[j];
DATA[j]=temp;
}
}
}
cout<<endl<<"SORTED ARRAY ELEMENTS ARE \n";
for(i=1;i<=N;i++)
{
cout<<endl<<"DATA ["<<i<<"] = "<<DATA[i];
}
cout<<endl<<"Enter number to search : ";
cin>>ITEM;
//LOGIC FOR BINARY SEARCH
LB=1;
UB=N;
while(LB<=UB)
{
MID=(LB+UB)/2;
if (ITEM==DATA[MID])
{
cout<<"The number is found at position "<<MID;
break;
}
if(ITEM>DATA[MID])
{
LB=MID+1;
}
if(ITEM<DATA[MID])
{
UB=MID-1;
}
}
if(LB>UB)
{
cout<<endl<<"The number is not found";
}
getch();
}
Output:-
Enter the dimension of array : 5
Enter the numbers of array :
22
44
11
33
55
SORTED ARRAY ELEMENTS ARE
DATA [1] = 11
DATA [2] = 22
DATA [3] = 33
DATA [4] = 44
DATA [5] = 55
Enter number to search : 44
The number is found at position 4
Program:-
# include<iostream.h>
# include<conio.h>
void main()
{
clrscr();
float A[5]={1.1,2.2,3.3,4.4,5.5};
float i,sum,*ptr;
sum=0;
ptr=A;
cout<<"Starting Address of the Array is "<<ptr;
cout<<endl<<"Size of number to which it points is
"<<sizeof(*ptr)<<" Bytes";
cout<<endl<<"----------------------------------------
----------------------";
for(i=1;i<=5;i++)
{
sum=sum+(*ptr);
cout<<endl<<"Address of A["<<i<<"] is "<<ptr<<"
Value at Address = "<<*ptr<<" Sum = "<<sum;
ptr++;
}
cout<<endl<<"----------------------------------------
----------------------";
cout<<endl<<"Ending Address of the Array is "<<--ptr;
getch();
}
Output:-
Starting Address of the Array is 0x8f57ffd8
Size of number to which it points is 4 Bytes
-----------------------------------------------------
---------
Address of A[1] is 0x8f57ffd8 Value at Address = 1.1
Sum = 1.1
Address of A[2] is 0x8f57ffdc Value at Address = 2.2
Sum = 3.3
Address of A[3] is 0x8f57ffe0 Value at Address = 3.3
Sum = 6.6
Address of A[4] is 0x8f57ffe4 Value at Address = 4.4
Sum = 11
Address of A[5] is 0x8f57ffe8 Value at Address = 5.5
Sum = 16.5
-----------------------------------------------------
---------
Ending Address of the Array is 0x8f57ffe8
Program:-
# include<iostream.h>
# include<conio.h>
# include<string.h>
void main()
{
clrscr();
char string[100],temp;
int i,len;
char *ptr_first,*ptr_last;
cout<<endl<<"Enter Any String "<<endl;
cin.get(string,100);
len=strlen(string);
ptr_first=&string[0];
ptr_last=&string[len-1];
for(i=0;i<=len;i++)
{
if(ptr_first<ptr_last)
{
temp=*ptr_first;
*ptr_first=*ptr_last;
*ptr_last=temp;
ptr_first++;
ptr_last--;
}
}
cout<<endl<<"Reverse String is "<<string;
getch();
}
Output:-
Enter Any String
RAJENDRA
Program:-
# include<iostream.h>
# include<conio.h>
class Ratio
{
private :
int num,den;
double ratio,rec,a,b;
public :
void assign();
void convert();
void invertor();
void print();
};
void Ratio :: assign()
{
cout<<endl<<"Enter Numerator = ";
cin>>num;
cout<<endl<<"Enter Denominator = ";
cin>>den;
}
void Ratio :: convert()
{
a=num;
b=den;
ratio=a/b;
}
void Ratio :: invertor()
{
rec=1/ratio;
}
void Ratio :: print()
{
cout<<endl<<"The Required ratio is "<<ratio;
cout<<endl<<"The Reciprocal of the ratio is
"<<rec;
}
void main()
{
Ratio R;
clrscr();
R.assign();
R.convert();
R.invertor();
R.print();
getch();
}
Output:-
Enter Numerator = 5
Enter Denominator = 7
Program:-
# include<iostream.h>
# include<conio.h>
class circle
{
private :
float a,c;
public :
float area(float r)
{
a=3.1415 * r * r;
return(a);
}
//-------------------------------
float circumference(float r)
{
c=2 * 3.1415 * r;
return(c);
}
}c1;
void main()
{
clrscr();
float r;
cout<<"Enter radius of a circle = ";
cin>>r;
cout<<endl<<"Area of a circle is "<<c1.area(r);
cout<<endl<<"Circumference of a circle is
"<<c1.circumference(r);
getch();
}
OUTPUT :-
Program:-
# include<iostream.h>
# include<conio.h>
class Ratio
{ private :
double m;
public :
Ratio();
Ratio(int a);
void Print();
};
Ratio::Ratio()
{
m=100;
}
//--------------------------
Ratio::Ratio(int a)
{
m=a;
m=1.0/m;
}
//--------------------------
void Ratio::Print()
{
cout<<endl<<"Ratio is "<<m;
}
void main()
{
clrscr();
Ratio I1;
Ratio I2(5);
I1.Print();
I2.Print();
getch();
}
Output:-
Ratio is 100
Ratio is 0.2
Program:-
# include<iostream.h>
# include<conio.h>
class Ratio
{
public :
Ratio(void);
void Display();
~Ratio(void);
};
Ratio::Ratio(void)
{
cout<<"OBJECT IS BORN ";
}
void Ratio::Display()
{
cout<<endl<<"NOW X IS ALIVE ";
}
Ratio::~Ratio(void)
{
cout<<endl<<"OBJECT DIES ";
getch();
}
void main()
{
clrscr();
Ratio X;
getch();
X.Display();
getch();
}
Output:-
OBJECT IS BORN
NOW X IS ALIVE
OBJECT DIES
Program:-
# include<iostream.h>
# include<conio.h>
class complex
{
private :
float x;
float y;
public :
complex();
complex(double real,double imag);
complex operator + (complex c);
void display(void);
};
complex::complex()
{
}
complex::complex(double real,double imag)
{
x=real;
y=imag;
}
complex complex::operator + (complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<y<<endl;
}
void main()
{
complex A,B,C;
clrscr();
A=complex(2.5,3.5);
B=complex(1.6,2.7);
C=A+B;
cout<<endl<<"A = ";
A.display();
cout<<endl<<"B = ";
B.display();
cout<<endl<<"SUM OF A & B = ";
C.display();
getch();
}
Output:-
A = 2.5+j3.5
B = 1.6+j2.7
Program:-
# include<iostream.h>
# include<conio.h>
class Ratio
{
private :
int x;
public :
Ratio();
Ratio(int a);
Ratio operator +(Ratio m);
Ratio operator /(Ratio m);
void Display();
};
Ratio::Ratio()
{
Ratio::Ratio(int a)
{
x=a;
}
void Ratio::Display()
{
cout<<x;
}
void main()
{
clrscr();
Ratio R1,R2,R3,R4;
R1=Ratio(50);
R2=Ratio(25);
R3=R1+R2;
R4=R1/R2;
cout<<endl<<"FIRST NUMBER IS ";R1.Display();
cout<<endl<<"SECOND NUMBER IS ";R2.Display();
cout<<endl<<"THEIR SUM IS ";R3.Display();
cout<<endl<<"THEIR DIVISION IS ";R4.Display();
getch();
}
Output:-
FIRST NUMBER IS 50
SECOND NUMBER IS 25
THEIR SUM IS 75
THEIR DIVISION IS 2
Program:-
# include<iostream.h>
# include<conio.h>
class Student
{
private :
int roll_number;
public :
void get_number()
{
cout<<endl<<"Enter Roll Number = ";
cin>>roll_number;
}
void put_number(void)
{
cout<<endl<<"ROLL NUMBER =
"<<roll_number;
}
};
class Test : public Student
{
protected :
float cs,maths;
public :
void get_marks()
{
cout<<endl<<"Enter Marks ";
cout<<endl<<"COMPUTER SCIENCE = ";
cin>>cs;
cout<<endl<<"MATHS = ";
cin>>maths;
}
void put_marks(void)
{
cout<<endl<<"Marks Obtained ";
cout<<endl<<"COMPUTER SCIENCE = "<<cs;
cout<<endl<<"MATHS = "<<maths;
}
};
class Sports
{
protected :
float score;
public :
void get_score()
{
cout<<endl<<"Enter score of spotrs = ";
cin>>score;
}
void put_score(void)
{
cout<<endl<<"SPORTS SCORE = "<<score;
}
};
class Result : public Test,public Sports
{
private :
float total;
public :
void Display(void)
{
total=cs+maths+score;
put_number();
put_marks();
put_score();
cout<<endl<<"TOTAL = "<<total;
}
};
void main()
{
clrscr();
Result R;
R.get_number();
R.get_marks();
R.get_score();
R.Display();
getch();
}
Output:-
Program:-
# include<iostream.h>
# include<conio.h>
class Person
{
public :
virtual void print()
{
cout<<endl<<"MY NAME IS BOB";
}
};
class Student : public Person
{
void print()
{
cout<<endl<<"MY NAME IS TOM";
}
};
void main()
{
clrscr();
Person X;
Student Y;
Person *p;
p=&X;
p->print();
p=&Y;
p->print();
getch();
}
Output:-
MY NAME IS BOB
MY NAME IS TOM
Output:-
NAME OF COUNTRYS
INDIA
UNITED STATES OF AMERICA
UNITED KINGDOM
JAPAN
CHINA
DELHI
WASHINGTON
LONDON
TOKYO
BEIJING