Sunteți pe pagina 1din 36

C++ PROGRAMS

PROGRAM 1 :

Write a program to check whether a triangle is valid or not, when the three angles of the
triangle are entered by the user.

#include<iostream.h>

#include<conio.h>

int main()

int angle1,angle2,angle3;

cout<<"\n Enter the three angles of triangle:";

cin>>angle1>>angle2>>angle3;

if (angle1+angle2+angle3==180)

cout<<"Triangle is valid";

else

cout<<"Triangle is not valid";

getch();

return 0;

1
OUTPUT 1 :

PROGRAM 2 :

Write a program to print all Armstrong numbers between 1 and 500.

#include<iostream.h>

#include<conio.h>

int main()

{ clrscr();

int n,digit1,digit2,digit3;

for(int i=1;i<=500;i++)

digit1=i/100;

digit2=i/10 - digit1*10;

digit3=i%10;

if(digit1*digit1*digit1 + digit2*digit2*digit2 + digit3*digit3*digit3 == i)

cout<<i<<endl;}

getch();

return 0;}

1
OUTPUT 2 :

PROGRAM 3 :

Write a program to print Fibonacci series of n terms where n is input by user.

#include<iostream.h>

#include<conio.h>

int main()

int f=0,s=1,t,n;

cout<<"Enter Number of terms of Series : ";

cin>>n;

cout<<f<<" "<<s<<" ";

for(int i=3;i<=n;i++)

t=f+s;

cout<<t<<" ";

f=s;

s=t; }

getch();

return 0;

1
OUTPUT 3 :

PROGRAM 4 :

Write a program to calculate the sum of following series where n is input by user.
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n

#include<iostream.h>

#include<conio.h>

int main()

{ clrscr();

int i,n;

float sum=0;

cout<<"Enter the value of n ";

cin>>n;

for(i=1;i<=n;i++)

sum += 1.0/i;

cout<<"Sum : "<<sum;

getch();

return 0;}

1
OUTPUT 4 :

PROGRAM 5 :

Write C++ program to print following pattern:

**********
**********
**********
**********

#include<iostream.h>

#include<conio.h>

int main()

{ clrscr();

int i,j;

for(i=1;i<=4;i++)

for(j=1;j<=10;j++)

cout<<'*';

cout<<endl;

getch();

return 0;

1
OUTPUT 5 :

PROGRAM 6 :

Write C++ program to print following pattern:

*
***
*****
*******
*********

#include<iostream.h>

#include<conio.h>

int main()

{ clrscr();

int i,j,k;

for(i=1;i<=5;i++)

for(j=5;j>i;j--)

cout<<' ';

for(k=1;k<2*i;k++)

cout<<'*';

cout<<endl; }

getch();

return 0; }

1
OUTPUT 6 :

PROGRAM 7 :

Write C++ program to print following pattern:

1
212
32123
4321234
543212345

#include<iostream.h>

#include<conio.h>

int main()

{ clrscr();

int i,j,k,l;

for(i=1;i<=5;i++)

{ for(j=5;j>i;j--)

cout<<' ';

for(k=i;k>=1;k--)

cout<<k;

for(l=2;l<=i;l++)

cout<<l;

cout<<endl;

getch();

return 0;}

1
OUTPUT 7 :

PROGRAM 8 :

Write a program to calculate the monthly telephone bills as per the following rule:
Minimum Rs. 200 for upto 100 calls.
Plus Rs. 0.60 per call for next 50 calls.
Plus Rs. 0.50 per call for next 50 calls.
Plus Rs. 0.40 per call for any call beyond 200 calls.

#include<iostream.h>

#include<conio.h>

void main()

{ int calls ;float bill;

cout<<"Enter number of calls : ";

cin>>calls;

if(calls<=100)

bill=200;

else if (calls>100 && calls<=150)

{ calls=calls-100;

bill=200+(0.60*calls); }

else if (calls>150 && calls<=200)

{ calls=calls-150;

bill=200+(0.60*50)+(0.50*calls);}

else

{ calls=calls-200;

bill=200+(0.60*50)+(0.50*50)+(0.40*calls);}

cout<<" Your bill is Rs."<<bill;

getch();

1
OUTPUT 8 :

PROGRAM 9 :

INSERTION SORT – 1 D ARRAY

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

void accept(int Arr[], int s);


void display(int Arr[], int s);
void isort(int Arr[], int s);

int main()
{
int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n;
do
{
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Sort the array using insertion sort method";
cout<<"\n3. Display elements of array";
cout<<"\n4. Exit";
cout<<"\n\nEnter your choice 1-3 :";
cin>>choice;

switch(choice)
{
case 1: accept(Arr,n);
break;
case 2: ssort(Arr,n);
break;
case 3: display(Arr,n);
break;
case 4: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=4);

1
getch();
return 0;
}

void accept(int Arr[], int s)


{
for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}

void isort(int Arr[], int s)


{
int I,J,Temp;
for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1;
while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}

void display(int Arr[], int s)


{
cout<<"The elements of the array are:\n";
for(int I=0;I<s;I++)
cout<<Arr[I]<<" ";
}

1
OUTPUT 9 :

PROGRAM 10 :

SELECTION SORT – 1D ARRAY

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

void accept(int Arr[], int s);


void display(int Arr[], int s);
void ssort(int Arr[], int s);

int main()
{
int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n;
do
{
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Sort the array using selection sort method";
cout<<"\n3. Display elements of array";
cout<<"\n4. Exit";
cout<<"\n\nEnter your choice 1-3 :";
cin>>choice;

1
switch(choice)
{
case 1: accept(Arr,n);
break;
case 2: ssort(Arr,n);
break;
case 3: display(Arr,n);
break;
case 4: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=4);
getch();
return 0;
}

void accept(int Arr[], int s)


{
for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}

void ssort(int Arr[], int s)


{
int I,J,Temp,Small;
for(I=0;I<s-1;I++)
{
Small=I;
for(J=I+1;J<s;J++) //finding the smallest element
if(Arr[J]<Arr[Small])
Small=J;
if(Small!=I)
{
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
}
}
}

void display(int Arr[], int s)


{
cout<<"The elements of the array are:\n";
for(int I=0;I<s;I++)
cout<<Arr[I]<<" ";
}

1
OUTPUT 10 :

Program 11:

BUBBLE SORT- 1D ARRAY

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

void accept(int Arr[], int s);


void display(int Arr[], int s);
void bsort(int Arr[], int s);

int main()
{
int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n;
do
{
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Sort the array using bubble sort method";
cout<<"\n3. Display elements of array";
cout<<"\n4. Exit";
cout<<"\n\nEnter your choice 1-3 :";

1
cin>>choice;

switch(choice)
{
case 1: accept(Arr,n);
break;
case 2: ssort(Arr,n);
break;
case 3: display(Arr,n);
break;
case 4: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=4);
getch();
return 0;
}

void accept(int Arr[], int s)


{
for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}

void bsort(int Arr[],int s)


{
int I,J,Temp;
for(I=0;I<s-1;I++)
{
for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}

void display(int Arr[], int s)


{
cout<<"The elements of the array are:\n";
for(int I=0;I<s;I++)
cout<<Arr[I]<<" ";
}

1
OUTPUT 11 :

Program 12 :

Write the definition for a class called time that has hours and minutes as integer. The class
has the following member functions:
void settime(int, int) to set the specified value in object
void showtime() to display time object
time sum(time) to sum two time object & return time

#include<iostream.h>

#include<conio.h>

class time

private:

int hours;

int minutes;

public:

void settime(int h, int m)

1
{

hours=h; minutes=m;

time sum(time);

void showtime();

};

time time::sum(time TM)

time t;

t.minutes = minutes + TM.minutes;

t.hours=t.minutes/60;

t.minutes=t.minutes%60;

t.hours += hours + TM.hours;

return t;

void time::showtime()

cout<<hours<<" hours and "<<minutes<<" minutes"<<endl;

int main()

{ clrscr();

time T1,T2,T3;

int x,y,w,v ;

cout<<"enter the first time , hours followed by minutes \n";

cin>>x;

cin>>y;

cout<<"\n enter the second time , hours followed by minutes \n" ;

1
cin>>w;

cin>>v;

T1.settime(x,y);

T2.settime(w,v);

T3=T1.sum(T2);

cout<<"\n Time 1 : ";T1.showtime();

cout<<"\n Time 2 : ";T2.showtime();

cout<<"\n SUM : ";T3.showtime();

getch();

return 0;

OUTPUT 12 :

1
PROGRAM 13 :

Write a C++ program to reverse the element of an integer 1-D array.

#include<iostream.h>

#include<conio.h>

int main()

{ clrscr();

int Arr[100],n,temp,i,j;

cout<<"Enter number of elements you want to insert ";

cin>>n;

for(i=0;i<n;i++)

cout<<"Enter element "<<i+1<<":";

cin>>Arr[i];

for(i=0,j=n-1;i<n/2;i++,j--)

temp=Arr[i];

Arr[i]=Arr[j];

Arr[j]=temp;

cout<<"\nReverse array"<<endl;

for(i=0;i<n;i++)

cout<<Arr[i]<<" ";

getch();

return 0;}

1
Output 13 :

PROGRAM 14 :

Write the definition for a class called Distance that has data member feet as integer and
inches as float. The class has the following member functions:
void set(int, float) to give value to object
void disp() to display distance in feet and inches
Distance add(Distance) to sum two distances & return distance

#include<iostream.h>

#include<conio.h>

class Distance{

private:

int feet;

float inches;

public:

void setdist(int ft, float in){

feet=ft;

inches=in;}

Distance add(Distance);

void disp();};

Distance Distance::add(Distance D){

Distance t;

t.inches=inches + D.inches;t.feet =0;

1
if(t.inches>=12.0){

t.inches-=12.0;

t.feet++;}

t.feet +=feet + D.feet;

return t;}

void Distance::disp(){

cout<<feet<<"\'"<<inches<<"\" ";}

int main()

{Distance d1,d2,d3;

d1.setdist(10,7.1);

d2.setdist(23,5.5);

d3=d1.add(d2);

cout<<"\n distance 1 = ";d1.disp();

cout<<"\n distance 2 = ";

d2.disp();

cout<<"\n distance 3 = ";

d3.disp();

getch();

return 0;}

OUTPUT 14 :

1
PROGRAM 15 :

#include<iostream.h>

#include<conio.h>

class complex{

private:

float x;

float y;

public:

void set(float real, float img){x=real; y=img;}

complex sum(complex);

void disp();};complex

complex::sum(complex C){

complex t;

t.x = x + C.x;

t.y = y + C.y;

return t;}

void complex::disp(){cout<<x<<" + j"<<y<<endl;}

void main(){

complex C1,C2,C3;C1.set(2.5,7.1);

C2.set(4.2,5.5);

C3=C1.sum(C2);

cout<<"\n complex Number 1 = ";

C1.disp();

cout<<"\n complex Number 2 = ";

C2.disp();

cout<<"\n complex Number 3 = ";

C3.disp();

getch();

1
OUTPUT 15 :

PROGRAM 16 :

Define a class batsman with the following specifications:


Private members:
bcode 4 digits code number
bname 20 characters
innings, notout, runs integer type
batavg it is calculated according to the formula –
batavg =runs/(innings-notout)
calcavg() Function to compute batavg
Public members:
readdata() Function to accept value from bcode, name, innings, notout and
invoke the function calcavg()
displaydata() Function to display the data members on the screen.

include<iostream.h>

#include<conio.h>

#include<stdio.h>

class batsman{

int bcode;

char bname[20];

int innings,notout,runs;

int batavg;

void calcavg(){batavg=runs/(innings-notout);}

public :

void readdata ();

void displaydata();}

;void batsman::readdata (){

1
cout<<"Enter batsman code ";

cin>> bcode;

cout<<"Enter batsman name ";

gets(bname);

cout<<"enter innings,notout and runs ";

cin>>innings>>notout>>runs;calcavg();}

void batsman::displaydata(){

cout<<"Batsman code "<<bcode<<"\nBatsman name "<<bname<<"\nInnings


"<<innings<<"\nNot out "<<notout<<"\nRuns "<<runs<<"\nBatting Average
"<<batavg;}

void main(){

batsman obj;

obj.readdata();

obj.displaydata();

getch();}

OUTPUT 17 :

1
PROGRAM 18 :

Searching a record IN A Binary file

#include<fstream.h>

#include<conio.h>

#include<stdlib.h>

class student{

int rollno;

char name[20];

char branch[3];

float marks;

char grade;

public:

void getdata(){

cout<<"Rollno: ";

cin>>rollno;

cout<<"Name: ";

cin>>name;

cout<<"Branch: ";

cin>>branch;

cout<<"Marks: ";

cin>>marks;

if(marks>=75){grade = 'A';}

else if(marks>=60){grade = 'B';}

else if(marks>=50){grade = 'C';}

else if(marks>=40){grade = 'D';}

else{grade = 'F';}}

void putdata(){

cout<<"Rollno: "<<rollno<<"\tName: "<<name<<"\n";

cout<<"Marks: "<<marks<<"\tGrade: "<<grade<<"\n";}

int getrno(){

return rollno;}}stud1;

1
void main(){

clrscr();

fstream fio("marks.dat", ios::in | ios::out);

char ans='y';

while(ans=='y' || ans=='Y')

{stud1.getdata();

fio.write((char *)&stud1, sizeof(stud1));

cout<<"Record added to the file\n";

cout<<"\nWant to enter more ? (y/n)..";

cin>>ans;}

clrscr();

int rno;

long pos;

char found='f';

cout<<"Enter rollno of student to be search for: ";

cin>>rno;

fio.seekg(0);

while(!fio.eof())

{pos=fio.tellg();

fio.read((char *)&stud1, sizeof(stud1));

if(stud1.getrno() == rno){

stud1.putdata();

fio.seekg(pos);

found='t';break;}}

if(found=='f'){

cout<<"\nRecord not found in the file..!!\n";

cout<<"Press any key to exit...\n";

getch();

exit(2);}

fio.close();

getch();}

1
OUTPUT 17 :

PROGRAM 18 :

INSERTING A RECORD IN BINARY FILE

#include<fstream.h>

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

class student{

int rollno;

char name[20];

char branch[3];

float marks;

char grade;

public:

void getdata()

{cout<<"Rollno: ";

1
cin>>rollno;

cout<<"Name: ";

cin>>name;

cout<<"Branch: ";

cin>>branch;cout<<"Marks: ";

cin>>marks;

if(marks>=75){grade = 'A';}

else if(marks>=60){grade = 'B';}

else if(marks>=50){grade = 'C';}

else if(marks>=40){grade = 'D';}

else{grade = 'F';}}

void putdata(){

cout<<"Rollno: "<<rollno<<"\tName: "<<name<<"\n";

cout<<"Marks: "<<marks<<"\tGrade: "<<grade<<"\n";}

int getrno(){return rollno;}}stud1, stud;

void main(){

clrscr();

ifstream fin("marks.dat", ios::in);

ofstream fout("temp.dat", ios::out);

char last='y';

cout<<"Enter details of student whose record is to be inserted\n";

stud1.getdata();

while(!fin.eof())

{fin.read((char*)&stud,sizeof(stud));

if(stud1.getrno()<=stud.getrno())

{fout.write((char *)&stud1, sizeof(stud1));

last ='n';

break;}

else{

fout.write((char *)&stud, sizeof(stud));}}

if(last == 'y'){

1
fout.write((char *)&stud1, sizeof(stud1));}

else if(!fin.eof()){while(!fin.eof()){fin.read((char *)&stud, sizeof(stud));

fout.write((char *)&stud,sizeof(stud));}}

fin.close();

fout.close();

remove("marks.dat");

rename("temp.dat", "marks.dat");

fin.open("marks.dat", ios::in);

cout<<"File now contains:\n";

while(!fin.eof()){

fin.read((char *)&stud, sizeof(stud));

if(fin.eof())

{break;}

stud.putdata();}

fin.close();getch();}

OUTPUT 18 :

1
PROGRAM 19 :

DELETING A RECORD FROM BINARY FILE

#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>

class file
{
private:
int roll;
float age;
char name[100];
public:
void input();
void show();
char *getn()
{
return name;
}
};file fileobj;

void file::input()
{
cout<<"Enter the roll, Age and name :";
cin>>roll>>age;
gets(name);
}
void file::show()
{
cout<<"Roll==> "<<roll<<endl;
cout<<"Age ==> "<<age<<endl;
cout<<"Name==> "<<name<<endl;
}
void Create();
void Display();
void Delete();
fstream fil;
int main()
{
int opt;
while(1)
{
clrscr();
cout<<"1.Create Data File"<<endl;
cout<<"2.Display Record From Data File"<<endl;
cout<<"3.Delete Particular Record From Data File"<<endl;
cout<<"4.Exit From the Program"<<endl;

1
cout<<"Enter your Option : "<<endl;
cin>>opt;
switch(opt)
{
case 1:
{
Create();
cout<<"Display Main Menu"<<endl;
getch();
break;
}

case 2:
{
Display();
cout<<"Display Main Menu"<<endl;
getch();
break;
}
case 3:
{
Delete();
cout<<"Display Main Menu"<<endl;
getch();
break;
}
case 4:
{
exit(0);
}
default:
{
cout<<"Wrong Choice....Press Key For View the Main Menu";
getch();
clrscr();
}
}
}
}

void Create()
{
char ch='y';
fil.open("binary.dat",ios::out| ios::binary);
while(ch=='y' || ch =='Y')
{
fileobj.input();
fil.write((char*)&fileobj, sizeof(fileobj));
cout<<"Want to Continue.....";
cin>>ch;
}
fil.close();

1
}

void Display()
{
fil.open("binary.dat",ios::in| ios::binary);
if(!fil)
{
cout<<"File not Found";
exit(0);
}
else
{
fil.read((char*)&fileobj, sizeof(fileobj));
while(!fil.eof())
{
fileobj.show();
cout<<"Press Any Key....For Next Record"<<endl;
getch();
fil.read((char*)&fileobj, sizeof(fileobj));
}
}
fil.close();
}

void Delete()

{
char n[100];
cout<<"Enter Name that should be Deleted :";
gets(n);
ofstream o;
o.open("new.dat",ios::out|ios::binary);
fil.open("binary.dat",ios::in| ios::binary);
if(!fil)
{
cout<<"File not Found";
exit(0);
}
else
{
fil.read((char*)&fileobj, sizeof(fileobj));
while(!fil.eof())
{
if(strcmp(n,fileobj.getn())!=0)
{
o.write((char*)&fileobj, sizeof(fileobj));
}
else
{
cout<<"Press Any Key....For Search"<<endl;
getch();
}

1
fil.read((char*)&fileobj, sizeof(fileobj));
}
}
o.close();
fil.close();
remove("binary.dat");
rename("new.dat", "binary.dat");
}

OUTPUT 19 :

1
PROGRAM 20 :
MODIFYING A RECORD IN A BINARY FILE

#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>

class file
{
private:
int roll;
float age;
char name[100];
public:
void input();
void show();
char *getn()
{
return name;
}
};file fileobj;

void file::input()
{
cout<<"Enter the roll, Age and name :";
cin>>roll>>age;
gets(name);
}
void file::show()
{
cout<<"Roll==> "<<roll<<endl;
cout<<"Age ==> "<<age<<endl;
cout<<"Name==> "<<name<<endl;
}
void Create();
void Display();
void Modify();
fstream fil;
int main()
{
int opt;
while(1)
{
clrscr();
cout<<"1.Create Data File"<<endl;
cout<<"2.Display Record From Data File"<<endl;
cout<<"3.Modify Particular Record From Data File"<<endl;
cout<<"4.Exit From the Program"<<endl;
cout<<"Enter your Option : "<<endl;
cin>>opt;

1
switch(opt)
{
case 1:
{
Create();
cout<<"Display Main Menu"<<endl;
getch();
break;
}

case 2:
{
Display();
cout<<"Display Main Menu"<<endl;
getch();
break;
}
case 3:
{
Delete();
cout<<"Display Main Menu"<<endl;
getch();
break;
}
case 4:
{
exit(0);
}
default:
{
cout<<"Wrong Choice....Press Key For View the Main Menu";
getch();
clrscr();
}
}
}
}

void Create()
{
char ch='y';
fil.open("binary.dat",ios::out| ios::binary);
while(ch=='y' || ch =='Y')
{
fileobj.input();
fil.write((char*)&fileobj, sizeof(fileobj));
cout<<"Want to Continue.....";
cin>>ch;
}
fil.close();
}

1
void Display()
{
fil.open("binary.dat",ios::in| ios::binary);
if(!fil)
{
cout<<"File not Found";
exit(0);
}
else
{
fil.read((char*)&fileobj, sizeof(fileobj));
while(!fil.eof())
{
fileobj.show();
cout<<"Press Any Key....For Next Record"<<endl;
getch();
fil.read((char*)&fileobj, sizeof(fileobj));
}
}
fil.close();
}

void Modify()
{
char n[100];
cout<<"Enter Name that should be searched:";
gets(n);
fil.open("binary.dat",ios::in| ios::out|ios::binary);
if(!fil)
{
cout<<"File not Found";
exit(0);
}
else
{
fil.read((char*)&fileobj, sizeof(fileobj));
while(!fil.eof())
{
if(strcmp(n,fileobj.getn())==0)
{
fil.seekg(0,ios::cur);
cout<<"Enter New Record.."<<endl;
fileobj.input();
fil.seekp(fil.tellg() - sizeof(fileobj));
fil.write((char*)&fileobj, sizeof(fileobj));
}
else
{
cout<<"Press Any Key....For Search"<<endl;
getch();}
fil.read((char*)&fileobj, sizeof(fileobj));}}
fil.close();}

1
OUTPUT 20 :

S-ar putea să vă placă și