Documente Academic
Documente Profesional
Documente Cultură
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;
cin>>angle1>>angle2>>angle3;
if (angle1+angle2+angle3==180)
cout<<"Triangle is valid";
else
getch();
return 0;
1
OUTPUT 1 :
PROGRAM 2 :
#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;
cout<<i<<endl;}
getch();
return 0;}
1
OUTPUT 2 :
PROGRAM 3 :
#include<iostream.h>
#include<conio.h>
int main()
int f=0,s=1,t,n;
cin>>n;
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;
cin>>n;
for(i=1;i<=n;i++)
sum += 1.0/i;
cout<<"Sum : "<<sum;
getch();
return 0;}
1
OUTPUT 4 :
PROGRAM 5 :
**********
**********
**********
**********
#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 :
*
***
*****
*******
*********
#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 :
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()
cin>>calls;
if(calls<=100)
bill=200;
{ calls=calls-100;
bill=200+(0.60*calls); }
{ 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);}
getch();
1
OUTPUT 8 :
PROGRAM 9 :
#include<iostream.h>
#include<conio.h>
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;
}
1
OUTPUT 9 :
PROGRAM 10 :
#include<iostream.h>
#include<conio.h>
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;
}
1
OUTPUT 10 :
Program 11:
#include<iostream.h>
#include<conio.h>
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;
}
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:
1
{
hours=h; minutes=m;
time sum(time);
void showtime();
};
time t;
t.hours=t.minutes/60;
t.minutes=t.minutes%60;
return t;
void time::showtime()
int main()
{ clrscr();
time T1,T2,T3;
int x,y,w,v ;
cin>>x;
cin>>y;
1
cin>>w;
cin>>v;
T1.settime(x,y);
T2.settime(w,v);
T3=T1.sum(T2);
getch();
return 0;
OUTPUT 12 :
1
PROGRAM 13 :
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
int Arr[100],n,temp,i,j;
cin>>n;
for(i=0;i<n;i++)
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:
feet=ft;
inches=in;}
Distance add(Distance);
void disp();};
Distance t;
1
if(t.inches>=12.0){
t.inches-=12.0;
t.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);
d2.disp();
d3.disp();
getch();
return 0;}
OUTPUT 14 :
1
PROGRAM 15 :
#include<iostream.h>
#include<conio.h>
class complex{
private:
float x;
float y;
public:
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 main(){
complex C1,C2,C3;C1.set(2.5,7.1);
C2.set(4.2,5.5);
C3=C1.sum(C2);
C1.disp();
C2.disp();
C3.disp();
getch();
1
OUTPUT 15 :
PROGRAM 16 :
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 displaydata();}
1
cout<<"Enter batsman code ";
cin>> bcode;
gets(bname);
cin>>innings>>notout>>runs;calcavg();}
void batsman::displaydata(){
void main(){
batsman obj;
obj.readdata();
obj.displaydata();
getch();}
OUTPUT 17 :
1
PROGRAM 18 :
#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{grade = 'F';}}
void putdata(){
int getrno(){
return rollno;}}stud1;
1
void main(){
clrscr();
char ans='y';
while(ans=='y' || ans=='Y')
{stud1.getdata();
cin>>ans;}
clrscr();
int rno;
long pos;
char found='f';
cin>>rno;
fio.seekg(0);
while(!fio.eof())
{pos=fio.tellg();
if(stud1.getrno() == rno){
stud1.putdata();
fio.seekg(pos);
found='t';break;}}
if(found=='f'){
getch();
exit(2);}
fio.close();
getch();}
1
OUTPUT 17 :
PROGRAM 18 :
#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{grade = 'F';}}
void putdata(){
void main(){
clrscr();
char last='y';
stud1.getdata();
while(!fin.eof())
{fin.read((char*)&stud,sizeof(stud));
if(stud1.getrno()<=stud.getrno())
last ='n';
break;}
else{
if(last == 'y'){
1
fout.write((char *)&stud1, sizeof(stud1));}
fout.write((char *)&stud,sizeof(stud));}}
fin.close();
fout.close();
remove("marks.dat");
rename("temp.dat", "marks.dat");
fin.open("marks.dat", ios::in);
while(!fin.eof()){
if(fin.eof())
{break;}
stud.putdata();}
fin.close();getch();}
OUTPUT 18 :
1
PROGRAM 19 :
#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 :