0% found this document useful (0 votes)
12 views

All - CPP - Programs

Uploaded by

gamersdevil6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

All - CPP - Programs

Uploaded by

gamersdevil6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Write a program in C++ that first initializes an array of given 10 real

numbers. The program must sort numbers in ascending/descending


order using bubble sort method. It should print the given list of
number as well as the sorted list.

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

Result: - Program was executed successfully.


Write a function in C++ that exchange data (pass by reference) using
swap function to interchange the given two numbers.

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:-

FROM MAIN FUNCTION


A = 10
B = 20
FROM SWAP FUNCTION
A = 20
B = 10
FROM MAIN FUNCTION
A = 20
B = 10

Result: - Program was executed successfully.


Write a program in C++ that first initializes an array of given, 10
sorted real number. The program must verify whether a given
element belong this array or not, using binary search technique. The
element (to be search) is to be entered at the time of execution. If the
number is found, the program should print its position in the array
otherwise it should print “The number is not found.”

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

Result: - Program was executed successfully.


Write a program in C++ that first initializes an array of five given
numbers (short/float/double). The program must add these numbers
by traversing this array with pointer. The output should print the
starting address of the array with the size of the number (in byte) to
which it points. The program must also print the sum and pointer
address with the addition of every number as well as the ending
address.

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

Result: - Program was executed successfully.


Write a function in C++ to input the given string (including
space) and reverse it using a function which locates the end
of string and swap the first character with the last character,
the second character with the second last and so on.

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

Reverse String is ARDNEJAR

Result: - Program was executed successfully.


Write a program in C++ with a ratio class using member functions like
assign( ) function to initialize its member data, integer numerator and
denominator, convert( ) function to convert the ratio into double,
invert ( ) to get the inverse of the ratio and print( ) function to print
the ratio and its reciprocal .

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

The Required ratio is 0.714286


The Reciprocal of the ratio is 1.4

Result: - Program was executed successfully.


Implement a circle class in C++ Each object of this class will represent
a circle, storing its radius. Include a default constructor, access
functions an area( ) function and circumference( ) function. The
program must print the radius, area and circumference of the circle.

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 :-

Enter radius of a circle = 3.5

Area of a circle is 38.483376


Circumference of a circle is 21.990499

Result: - Program was executed successfully.


Write a program in C++ that initializes a Ratio class with no
parameters as a default constructor. Add a constructor having no
parameters that initializes the declared object with the default
integer values --- and--- Add another constructor that has one integer
parameter. This constructor must initialize the object to be functional
equivalent of that integer. The output should print the ratio as per the
constructors.

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

Result: - Program was executed successfully.


Write a program in C++ that initializes a Ratio class with no
parameters as a default constructor. The program must print the
message “OBJECT IS BORN” during initialization. It should display the
message “NOW X IS ALIVE”, when the first member function Ratio x is
called. The program must display “OBJECT DIES”, when the class
destructor is called for the object when it reaches the end of its scope.

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

Result: - Program was executed successfully.


Write a program in C++ with a complex constructor to add the given
two complex numbers
A = ----------- and B = ------------- The program should print the given
complex numbers and their sum.

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

SUM OF A & B = 4.1+j6.2


Write a program in C++ to implement the addition and division
operator for the Ratio class. Hence print the given two ratios x and y,
their sum (x+y) and their division (x/y).

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;
}

Ratio Ratio::operator +(Ratio m)


{
Ratio Temp;
Temp.x=x+m.x;
return(Temp);
}

Ratio Ratio::operator /(Ratio m)


{
Ratio Temp;
Temp.x=x/m.x;
return(Temp);
}

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

Result: - Program was executed successfully.


Write a Program in C++ to read student’s particular such as roll
number, class test to obtain marks scored in two different subjects,
class sports to obtain weightage (marks) in sports and class Result to
calculate the total marks. The program must print roll number;
individual marks obtain in two subjects, sports and total marks.

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:-

Enter Roll Number = 3011


Enter Marks
COMPUTER SCIENCE = 189
MATHS = 95
Enter score of sports = 25

ROLL NUMBER = 3011


Marks Obtained
COMPUTER SCIENCE = 189
MATHS = 95
SPORTS SCORE = 25
TOTAL = 309

Result: - Program was executed successfully.


Write a program in C++ using virtual function. The program must
declare p to be a pointer to objects of the base class Person . First, the
program must assign p to point an instance x (name of the person eg.
“BOB”) of class person. The program must then assign p to point at an
instance y (name of the student, eg. “TOM” ) of the derived class
Student.
Define a print ( ) function in the base class such that it invokes the
same base class function to print the name of the person by default
.The second call for the same should evoke the driver class function to
print the name of the student.

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

Result: - Program was executed successfully.


Write a program in C++ to read the name of a country from one text
file and name of its corresponding capital city from another text file.
The program must display the country name and indicate its
corresponding capital (for at least five countries) in the output.
Program:-
# include<iostream.h>
# include<conio.h>
# include<fstream.h>
void main()
{
clrscr();
ofstream fout;
fout.open("COUNTRY");
fout<<endl<<"INDIA";
fout<<endl<<"UNITED STATES OF AMERICA";
fout<<endl<<"UNITED KINGDOM";
fout<<endl<<"JAPAN";
fout<<endl<<"CHINA";
fout.close();
fout.open("CAPITAL");
fout<<endl<<"DELHI";
fout<<endl<<"WASHINGTON";
fout<<endl<<"LONDON";
fout<<endl<<"TOKYO";
fout<<endl<<"BEIJING";
fout.close();
const int n=80;
char line[n];
ifstream fin;
cout<<endl<<"NAME OF COUNTRYS \n";
fin.open("COUNTRY");
while(fin.eof()==0)
{
fin.getline(line,n);
cout<<line<<endl;
}
fin.close();
cout<<endl<<"RESPECTIVE CAPITAL'S OF COUNTRYS
\n";
fin.open("CAPITAL");
while(fin.eof()==0)
{
fin.getline(line,n);
cout<<line<<endl;
}
fin.close();
getch();
}

Output:-

NAME OF COUNTRYS

INDIA
UNITED STATES OF AMERICA
UNITED KINGDOM
JAPAN
CHINA

RESPECTIVE CAPITAL'S OF COUNTRYS

DELHI
WASHINGTON
LONDON
TOKYO
BEIJING

You might also like