You are on page 1of 58

C++ Revision Tour

Q- Write a program to find the LCM and GCD of two numbers. #include<iostream.h> #include<conio.h>

void main() { clrscr(); int x,y,gcd=1; cout<< "ENTER 1st NO : "; cin>>x; cout<<"\n\nEnter 2nd NO. :"; cin>>y; for(int i=1;i<1000;++i) { if((x%i==0)&&(y%i==0)) gcd=i; } cout<<"\n\n\nGCD :"<<gcd; cout<<"\n\n\nLCM :"<<(x*y)/gcd; getch(); } OUTPUT: ENTER 1st NO : 12 ENTER 2nd NO. :13 GCD :1 LCM :156

Q- Write a program to find the sum of sequence 1 + 1/1! + 1/2!+ ......... #include<iostream.h> #include<conio.h> void main() { int n,i; float sum=1.00; float fact(int a); clrscr(); cout<<"Enter n:"; cin>>n; for(i=1;i<n;i++) { sum+=(1/fact(i)); } cout<<"Sum of series ="<<sum; getch(); } float fact (int a) { int f=1,i; for (i=1;i<=a;i++) f*=i; return f; } OUTPUT: Enter n : 5 Sum of series =2.708333

Q- Write a function that takes time as three integer arguments(hours,minutes,seconds), and returns the number of seconds since the clock last struck "12". Use this function to write a program to calculate the amount of time in seconds between two times, both of which are within one 12 hour cycle of clock. #include<iostream.h> #include<conio.h> #include<math.h> int seconds(int hh,int mm,int ss) { int z,s; z=abs(hh-12); s=(z*60*60)+(mm*60)+ss; return s; } int main() { clrscr(); int h1,h2,m1,m2,s1,s2,t1,t2; cout<<"Enter the first time:"<<endl; cout<<"\tHours:"; cin>>h1; cout<<"\tMinutes:"; cin>>m1; cout<<"\tSeconds:"; cin>>s1; t1=seconds(h1,m1,s1); cout<<"Enter the second time:"<<endl; cout<<"\tHours:"; cin>>h2; cout<<"\tMinutes:"; cin>>m2; cout<<"\tSeconds:"; cin>>s2; t2=seconds(h2,m2,s2); cout<<"The difference in time is:"<<abs(t1-t2); return 0; } OUTPUT:

Enter the first time: Hours:5 Minutes:45 Seconds:59 Enter the second time: Hours:7 Minutes:12 Seconds:5 The difference in time is:9234

Write a program to find the sum of the sequence #include<iostream.h> #include<conio.h> #include<math.h> void sum(int x,int n) { double sum=0,s=0; int k=1; long fac=1; for(int i=1;i<=n-1;i++) { for(int j=(2*k);j>=1;j--) { fac*=j; } sum+=(pow(x,i)/fac); } s=1+sum; cout<<"\nThe sum is:\n"; cout<<s; } void main() { clrscr(); int x,n; cout<<"\nEnter the limiting value\n"; cin>>n; cout<<"\nEnter the value \n"; cin>>x; sum(x,n); getch(); } OUTPUT: Enter the limiting value 5 Enter the value 3 The sum is: 13.1875

if(ans!=(x[0]*x[1])) cout<<"\nWRONG! TRY AGAIN\n". #include<iostream. x[0]=rand()%10. cin>>c.h> #include<stdlib. } if(c=='N'||c=='n') { break. Write aprogram that will help elementary school students leaern multiplication. Use rand function to produce two positive one digit integers. int x[2]. }while(ans!=(x[0]*x[1])). if(ans==(x[0]*x[1])).Q. char c. } . do { cin>>ans.ans. time_t t. do { srand(time(&t)).Computers are playing an increasing role in education.} }while(1). cout<<"\n\nWhat is "<<x[0]<<"times "<<x[1]<<" ?\nANS: ". { cout<<"correct!\n\n\nDO YOU WANT TO CONTINUE?". srand(x[0]).h> void main() { clrscr().h> #include<time.h> #include<conio. x[1]=rand()%10.

OUTPUT: What is 5times 1 ? ANS: 5 correct! DO YOU WANT TO CONTINUE?y What is 6times 7 ? ANS: 48 WRONG! TRY AGAIN 42 correct! DO YOU WANT TO CONTINUE?n .

Write a program to accept three digits and print all possible combinations from these digits. clrscr().j<3.k++) { if (i!=j&&i!=k&&j!=k) cout<<endl<<endl<<a[i]<<a[j]<<a[k].j++) { for (int k=0. cin>>a[0]>>a[1]>>a[2].k<3. #include<iostream.h> void main() { int a[3]. cout<<"Enter three digits :"<<endl.Q. } } } getch().i<3.i++) { for(int j=0. } OUTPUT: Enter three digits : 123 123 132 213 231 312 321 . for (int i=0.h> #include<conio.

j++) { if (i!=j) { if (arr[i]==arr[j]) arr[j]=0. } for (i=0. Provide for the worst case in which all 20 integers are different.i++) { cout<<"enter the element no "<<i+1<<endl. } } for (i=0.h> void main() { clrscr(). } . #include<iostream.i<20. else continue.h> #include<conio.i++) { for (int j=0. for (int i=0. } getch().Use one dimensional array to solve the following proble. As each number is read. } else continue.i<20.Q . Read 20 integers from data file. int arr[20]. each of which is between 10 and 100.i++) { if (arr[i]<=100&&arr[i]>=10) cout<<arr[i]<<endl. inclusive.i<20. else continue.j<20. Use the smallest possible array to solve this problem. print it only if it is not a duplicate of a number already erad. cin>>arr[i].

OUTPUT: enter the element no 1 1 enter the element no 2 11 enter the element no 3 12 enter the element no 4 113 enter the element no 5 14 enter the element no 6 15 enter the element no 7 16 enter the element no 8 17 enter the element no 9 18 enter the element no 10 19 enter the element no 11 10 enter the element no 12 11 enter the element no 13 12 enter the element no 14 13 enter the element no 15 14 enter the element no 16 15 enter the element no 17 16 enter the element no 18 17 enter the element no 19 23 enter the element no 20 34 11 .

12 14 15 16 17 18 19 10 13 23 34 .

i++.. #include<iostream.p. cout<<"Enter till what power you want:".i<=p. cin>>p.h> #include<math.. } cout<<"The sum of the series is:"<<ans. } OUTPUT: Enter the value of x:3 Enter till what power you want:4 The sum of the series is:-4.j++){ fact=fact*i.Q Write a c++ program to sum the sequence x -(x^2/2!) + (X^4/4!) -(x^6/6!) + .i))/(fact)). for(i=2. ans=x.h> int main() { int x.. double fact=1. if(i%2==0) ans+=(pow(-1.ans=0.j.j))*((pow(x.j=1.875 . cin>>x..0..i. return 0.h> #include<conio. cout<<"Enter the value of x:"..

ans=1.h> int main() { int x.0. cin>>x.. } OUTPUT: Enter the value of x:3 Enter till what power you want:4 The sum of the series is:1.. double fact=1.p.. #include<iostream.i))*((pow(x... ans+=(pow(-1. cin>>p. for(i=1..375 .i))/(fact)). cout<<"Enter till what power you want:".i.(U^2) + 1/2!(U^2) -1/3!(U^3) + ..i++){ fact=fact*i.h> #include<math....j. cout<<"Enter the value of x:".Q Write a c++ function having two value parameters U and n with result type float to find the sum of the series given below 1 .h> #include<conio. return 0. } cout<<"The sum of the series is:"<<ans.i<=p...

Structures .

cin>>phone. gets(spousename). } void getname() { cout<<"\nEnter the name\n". cout<<"\nEnter the phone number\n". } void display() { cout<<"\nName\t".Write a program to accept information about a person and then display it . }. class spouse:public person { char spousename[40]. cout<<"\nPhone number\t"<<phone. cout<<"\nEnter the spouse name\n". void display(). public: person() { cout<<"\nConstructor initialised\n". } }.puts(name). void spouse::getsp() { person::getname().h> #include<stdio. public: void getsp().h> class person { char name[30].h> #include<conio. #include<iostream. } ~person() { cout<<"\n******************\n". } . long phone.Q. gets(name).h> #include<string.

} OUTPUT: Constructor initialised Enter the name ARUN Enter the phone number 6667070 Enter the spouse name ARUNA Name ARUN Phone number 6667070 Spouse name ARUNA ****************** . obj. getch(). cout<<"Spouse name\t".getsp(). obj.void spouse::display() { person::display().puts(spousename). } void main() { clrscr(). spouse obj.display().

gets (pl1[i].runs scored. int plytyp. bowl pl2[3]. indica. }. cin>>pl1[i].i++) { cout<<"Enter name of player "<<i+1<<endl.name).h> #include<stdio. bat pl1[3]. it displays either batting team's information or bowling team's information. One array stores information of batting team such as batman's name.modeout[70].rnsgvn.runs.Write a program to record a score of a cricket match. cout<<"Enter the status of the player if out (N)or not(Y)"<<endl. score. struct bowl { char name[20]. }. xtras. cout<<"Enter the overs played by the player"<<i+1<<endl.totove. #include<iostream. cin>>pl1[i].h> #include<conio. int ttvrs.i<3. cin>>pl1[i]. totove. void main() { clrscr(). cout<<"Enter the batsmen details:"<<endl. for (int i=0.wktstkn.Q. totruns.etc.h> struct bat { char name[20].indica. The progdram reads in above informatin and depending on user's choice. The other array stores information about bowling team. int plno. cout<<"enter the runs scored by player "<<i+1<<endl. } . int runs.

name).totove<<endl. puts(pl1[plno].indica<<endl.wktstkn. cin>>pl2[i]. cout<<"Enter the runs given by the bowler "<<i+1<<endl. plno--. case 2: cout<<"Enter the bowlers number to see his details "<<endl<<endl<<endl. break. cout<<"Player status out "<<pl1[plno]. cout<<"Bowlers name :". plno--. cout<<"Runs scored by the batsman :"<<pl1[plno]. } cout<<"Thank you all details recd"<<endl.cout<<"Enter the bowlers details "<<endl.name). cout<<"Enter the wickets taken by the bowler "<<i+1<<endl. for (i=0. abc: cin>>plytyp. cin>>plno. cin>>plno.runs<<endl.i++) { cout<<"Enter the name of the bowler "<<i+1<<endl.i<3. cout<<"Batsman number :"<<plno+1<<endl. xyz: cout<<"Select between batsmen(1) or bowlers(2) to see their details"<<endl. cin>>pl2[i]. cout<<"Batsman name :". cin>>pl2[i]. gets(pl2[i]. cout<<"Total overs played by the batsman :"<<pl1[plno]. switch (plytyp) { case 1: cout<<"Enter the batsman number to see his details "<<endl<<endl<<endl.ttvrs.rnsgvn. cout<<"Enter the total overs played by the bowler "<<i+1<<endl. .

rnsgvn<<endl. } output: Enter the batsmen details: Enter name of player 1 SACHIN enter the runs scored by player 1 34 Enter the overs played by the player1 6 Enter the status of the player if out (N)or not(Y) N Enter name of player 2 GAMBHIR enter the runs scored by player 2 12 Enter the overs played by the player2 5 Enter the status of the player if out (N)or not(Y) Y Enter name of player 3 SEHWAG enter the runs scored by player 3 56 Enter the overs played by the player3 11 .puts(pl2[plno]. break. goto abc. if (plno==1) goto xyz. cout<<"Total wickets taken by the user :"<<pl2[plno].ttvrs<<endl.wktstkn<<endl. cout<<"Runs given by the player is :"<<pl2[plno]. else cout<<"Thank you Press any key to exit". cin>>plno. getch().name). cout<<"Total overs played by the player :"<<pl2[plno]. default: cout<<"Idiot enter a decent value"<<endl. } cout<<endl<<endl<<endl<<"Do you wish to continue? Y-1 N-2"<<endl.

Enter the status of the player if out (N)or not(Y) N Enter the bowlers details Enter the name of the bowler 1 31 Enter the wickets taken by the bowler 1 1 Enter the total overs played by the bowler 1 5 Enter the name of the bowler 2 JOHNSON Enter the runs given by the bowler 2 25 Enter the wickets taken by the bowler 2 0 Enter the total overs played by the bowler 2 4 Enter the name of the bowler 3 MCGRATH Enter the runs given by the bowler 3 40 Enter the wickets taken by the bowler 3 0 Enter the total overs played by the bowler 3 7 Thank you all details recd Select between batsmen(1) or bowlers(2) to see their details 2 Enter the bowlers number to see his details 2 Bowlers name :JOHNSON Runs given by the player is :25 Total overs played by the player :4 Total wickets taken by the user :0 Do you wish to continue? Y-1 N-2 Y Select between batsmen(1) or bowlers(2) to see their details Enter the bowlers number to see his details Bowlers name :LEE .

Runs given by the player is :31 Total overs played by the player :5 Total wickets taken by the user :1 Do you wish to continue? Y-1 N-2 Thank you Press any key to exit .

Object Oriented Programming .

Q.i++) res*=n.n). return res. #include<iostream. cout<<"The square of the numberis :"<<power(x. return res. The first version takes double n and int p and returns a double value. Use default value of 2 for p in case p is ommitted in the function call.i<p. int n. } int power (int n.h> #include<conio. getch(). cout<<"The answer is :"<<ans<<endl. Write an overloaded functions having two versions for it.i<p. Another version takes int n and int p returning int value. cout<<"enter the raising value"<<endl.i++) res*=n.int p) { double res=1. cout<<"enter the number"<<endl. clrscr(). cin>>x.2). ans=power(x.ans. for (int i=0.Raising a number n to a power p is the same as multiplying n by itself p times. } OUTPUT: enter the number 3 enter the raising value 4 The answer is :81 The square of the numberis :9 . } void main() { double x. for (int i=0.h> double power (double n. int p) { int res=1. cin>>n.

#include<iostream. cin>>b. void inquote(double y). cin>>a. inquote(b). Write three versions: one for an int argument. int a. case 3: cout<<"enter the character"<<endl. } getch(). break. cin>>s. void main() { clrscr().c. a function that displays its arguments enclosed in double quotation marks. switch (c) { case 1: cout<<"Enter the number"<<endl. inquote(a). double b. break. one for double argument and one for char argument. inquote(s).Write overloaded prototypes of inquote().char"<<endl. case 2: cout<<"enter the double value"<<endl.h> void inquote(int x).double"<<endl<<"3. cout<<"Enter the choice"<<endl<<"1. char s.h> #include<conio. } void inquote(int x) { cout<<"\""<<x<<"\""<<endl.number"<<endl<<"2. cin>>c. } . void inquote(char s). break.Q.

void inquote(double y) { cout<<"\""<<y<<"\""<<endl. } void inquote(char s) { cout<<"\""<<s<<"\""<<endl. } OUTPUT: Enter the choice 1.double 3.number 2.char 3 enter the character T "T" .

h of box ". volume(l. volume(r. cout<<"\nEnter side of cube ".b.hc. } void main() { clrscr(). cout<<"\nVolume of cube is "<<vol.int h) { vol=(l*b*h).h). cout<<"\nVolume of cylinder is "<<vol.h of box 6 7 8 Enter side of cube 5 . a function that returns volumes of different structures. void volume(int r.int b.Q. #include<iostream.h> double vol. cout<<"\nVolume of box is "<<vol.b.Write overloaded prototypes of themax(). cin>>s.b.r.b.hc).14*r*r*hc). cin>>l>>b>>h. volume(s). cout<<"\nEnter radius and height of cylinder ". } void volume(int l. getch().l. one for cylinder's volume and one for rectangular box's volume. int s. } OUTPUT: Enter radius and height of cylinder 4 7 Enter l. cout<<"\nEnter l. } void volume(int s) { vol=(s*s*s).h.h> #include<conio. cin>>r>>hc. Write three versions : one for cube's volume.int hc) { vol=(3.

Volume of cylinder is 351.68 Volume of box is 336 Volume of cube is 125 .

Classes And Objects .

b.givecount().c.c3.i<10.ladies(l). } void incount() { count++. } }c1.e gentlemen or ladies or children using a class. void main() { clrscr().givecount().incount(). char guest[10].a. } else . cin>>guest[i]. } else if(guest[i]=='l'||guest[i]=='L') { c2.incount().h> #include<stdio. public: counter() { count=0.chldrn(c) "<<endl. int i.h> #include<conio. b=c2.h> class counter { int count. if(guest[i]=='g'||guest[i]=='G') { c1.Q.i++) { cout<<"enter gntlmn(g).Write a program to calculate the no. a=c1.c2. #include<iostream. for(i=0. of types of guests i. } int givecount() { return (count).

chldrn(c) l enter gntlmn(g).chldrn(c) l enter gntlmn(g).ladies(l).ladies(l). } } cout<<"GENTLEMEN :"<<a<<endl. c=c3.chldrn(c) g enter gntlmn(g).chldrn(c) l enter gntlmn(g).ladies(l).givecount().ladies(l). } OUTPUT: enter gntlmn(g).chldrn(c) g enter gntlmn(g). cout<<"LADIES :"<<b<<endl.chldrn(c) c enter gntlmn(g).ladies(l).ladies(l).{ c3.ladies(l).incount(). cout<<"CHILDREN :"<<c<<endl. getch().chldrn(c) c GENTLEMEN :2 LADIES :5 CHILDREN :3 .ladies(l).chldrn(c) l enter gntlmn(g).chldrn(c) l enter gntlmn(g).ladies(l).ladies(l).chldrn(c) c enter gntlmn(g).

public: void inputdata(). cout<<"\nenter total annual taxable income:".Q. cout<<"\nenter the name of the person:". puts(name).Write a program to compute tax of a person using a class taxpayer. else if((tableinc>60000)||(tableinc<=150000)) tax= tableinc *0.cout<<"\ttotal annual income:"<<tableinc<<"\tTax payable:"<<tax. } double taxpayer::computetax() { if(tableinc<=60000) tax=0. double tax. cin>>tableinc. } void taxpayer::display() { cout<<"\npan num:"<<pan<<"\tname:". long float tableinc. void display().15. #include<iostream.h> #include<stdio.h> class taxpayer{ int pan. cin>>pan. void taxpayer::inputdata() { cout<<"enterpersonal acct num:". }. Class taxpayer should the person's details of account including pan no.05. double computetax(). else tax=tableinc*0. gets(name).h> #include<conio. else if(tableinc>150000||tableinc<=500000) tax=tableinc*0. } void main() { clrscr(). char name[20]. .1. return (tax).

computetax(). a. clrscr(). a.taxpayer a. } OUTPUT: enterpersonal acct num:121 enter the name of the ethiopian:ARUN enter total annual taxable income:120000 pan num:121 name:ARUN total annual income:120000 Tax payable:6000 . getch(). a.display().inputdata().

of objects created of a particular class type. public: void enter() { cout<<"Enter a value of x:".Q. cin>>ch. } OUTPUT: Enter a value of x:4 Do you want to continue:y Enter a value of x:7 Do you want to continue:n The number of objects created are:2 . while(ch=='y') { j. } }. char ch='y'. #include<iostream.h> #include<conio. i++. cout<<"Do you eant to continue:". } cout<<"The number of objects created are:"<<i. int i=0.Wite a c++ program to calculate the no. } void out() { cout<<"The value is:"<<x<<endl. cin>>x. return 0.h> class just { int x.enter(). int main() { just j.

int marks[5]. for(int i=0.h> class student { int roll_no. cin>>class_st. } void student::readmarks() { cout<<"Enter the roll no. char name[20]. public: void readmarks().i<5. cin>>name. for(int j=0.:". float student::calculate() { percentage=0. cin>>marks[j].h> #include<conio. cout<<"Enter the marks:"<<endl. } } .Write a c++ program using a class student to calculate the percentage of marks obtained by him.j<5. return percentage. #include<iostream. float percentage. percentage=(percentage/5). void displaymarks(). char class_st[8]. cin>>roll_no.Q. float calculate().i++) percentage+=marks[i]. cout<<"Enter the class studing in:".j++){ cout<<"\tEnter mark "<<j+1<<":". }. cout<<"Enter the name:".

s1. } OUTPUT: Enter the roll no. } int main() { student s1. s1.599998 .void student::displaymarks() { cout<<"Roll no:"<<roll_no<<endl. cout<<"Percentage:"<<calculate()<<endl.displaymarks(). return 0.readmarks(). cout<<"Name:"<<name<<endl.:12 Enter the name:KARTHIK Enter the class studing in:12 Enter the marks: Enter mark 1:99 Enter mark 2:95 Enter mark 3:90 Enter mark 4:80 Enter mark 5:99 Roll no:12 Name:KARTHIK Class:12 Percentage:92. cout<<"Class:"<<class_st<<endl.

float duration. public: serial() { duration=30. duration. noe=103. cout<<"Enter the title:".serial code. of episodes are:"<<noe<<endl<<endl. cout<<"\tDurations is:"<<duration<<endl. void displaydata(). } void newserial(). char title[20]. } void serial::displaydata() { cout<<"\tSerial code is:"<<code<<endl. noe=no. void otherentries(int dur.Write a c++ program using class serial to store and display serial's title.h> #include<conio.int no) { duration=dur.h> class serial { int code. cout<<"\tTitle is:"<<title<<endl. }. #include<iostream. } void serial::otherentries(int dur. void serial::newserial() { cout<<"\n\nEnter the serial code:". .int no).Q. int noe. cout<<"\tNo. number of episodes.h> #include<stdio. cin>>code. gets(title).

cin>>dur>>no. cout<<"\n\nDo you want to continue:". i++. of episodes:60 20 Do you want to continue:n . cout<<"Enter the duration and the no. } OUTPUT: Enter the serial code:121 Enter the title:DaVinciCode Enter the duration and the no. int i=0. s1[j]. s1[i]. cin>>ch. } return 0.j++){ cout<<"Data of serial "<<j+1<<" is:"<<endl. for(int j=0.otherentries(dur. while(ch=='y') { s1[i].} int main() { clrscr().no). of episodes:".j<i. } cout<<"\n\nThe details you have entered are:"<<endl<<endl.displaydata().newserial().no. of episodes:30 50 Do you want to continue:y Enter the serial code:122 Enter the title:solomon Enter the duration and the no.dur. char ch='y'. serial s1[10].

The details you have entered are: Data of serial 1 is: Serial code is:121 Title is:DaVinciCode Durations is:30 No. of episodes are:20 . of episodes are:50 Data of serial 2 is: Serial code is:122 Title is:solomon Durations is:60 No.

Perform calculations on two operands using a class calculator. void add(). }. cout<<"Enter the other operant:". void mul(). int o1.h> #include<process. void calculator::enter() { cout<<"Enter a operant:". void sub(). #include<iostream. void clear().Q.Write a c++ program to illustrate a calculator. public: void enter(). cin>>o2. multiply and divide operands. } void calculator::add() { result=o2+o1.o2.h> #include<conio.h> class calculator { float result.subtract. } void calculator::sub() { . void showresult(). } void calculator::showresult() { cout<<"The result of the operation is:"<<result<<endl. cin>>o1. Calculator should add. void div().

sub().showresult(). cin>>ch.result=o1-o1. break. case '*':c1. } int main() { char ch='y'.add(). switch(ch) { case '+':c1. exit(0). break. case '-':c1. } void calculator::mul() { result=o1*o2. case '/':c1.mul(). break. } void calculator::div() { result=o1/o2.enter(). } c1. default :cout<<"Wrong choice:". break. calculator c1. } void calculator::clear() { result=0.div(). while(ch=='y') { c1. . cout<<"Which operation do you want to perform:".

} OUTPUT: Enter a operand:4 Enter the other operand:8 Which operation do you want to perform:* The result of the operation is:32 Do you want to continue:y Enter an operand:4 Enter the other operand:8 Which operation do you want to perform:+ The result of the operation is:12 Do you want to continue:n .clear(). cin>>ch. cout<<"Do you want to continue:". } return 0.c1.

h> #include<process. }. of people and the total have beem incremented". cout<<"The total amount collected till now is:"<<total<<endl.50. public: tick() { nop=0. cout<<"\nThe no. The booth keeps track of the number of people that have visited the fair and of the total amount of money collected. A ticket is priced at Rs. 2.h> #include<conio. #include<iostream.Q. } void inc(). void tick::inc() { nop++.50. } void tick::display() { cout<<"\nThe number of people who have entered the fair are:"<<nop<<endl.Imagine a ticket selling booth at a fair. of people who have visited so far are:"<<nop<<endl. float total. Include a program to rest this class. Model this ticketselling booth with a class tick. . } void tick::displaynop() { cout<<"The no. void display(). void displaynop(). total=0. People passing by are requested to purchase a ticket.h> class tick { int nop. total+=2.

break. cout<<"4. tick t1. break.Display no.Increment person and total"<<endl. goto l1. l1:cout<<"\n\n\n\n1. case 2:t1. cout<<"Enter your choice:". cout<<"3.inc(). goto l1. break.Display no. goto l1. case 3:t1.display().Exit"<<endl. break. of people and the amount collected till now"<<endl. of people and the total have beem incremented .Display no. cout<<"2. of people and the amount collected till now 3.} int main() { char ch='y'. cin>>choice. switch(choice) { case 1:t1.displaynop(). } return 0. of people who entered 4. of people who entered"<<endl.Increment person and total 2.Display no. } OUTPUT: 1. case 4:exit(0).Exit Enter your choice:1 The no. int choice.

of people and the amount collected till now 3. of people and the amount collected till now 3. of people who entered 4.Increment person and total 2.Display no. of people and the amount collected till now 3.Display no.Exit Enter your choice:1 The no. of people who have visited so far are:2 1.Display no.Display no. of people who entered 4.Display no. of people who entered 4.Display no.Display no.1.Display no.Exit Enter your choice:4 .Exit Enter your choice:2 The number of people who have entered the fair are:2 The total amount collected till now is:5 1. of people and the total have beem incremented 1.Increment person and total 2. of people and the amount collected till now 3. of people who entered 4.Exit Enter your choice:3 The no.Increment person and total 2.Increment person and total 2.

Constructors And Destructors .

numcopies=50. The list includes details such as author. int numcopies. double price. stock::stock() { char author[50]={"abc"}.h> #include<stdio.A book shop maintains the inventory of books that are being sold at the shop. else return 1. int access_title(char a[]).a)) return 0. else cout<<"\nSorry! These many copies are unavailable!". } . price.h> #include<conio. } void stock::getdata(int num) { if(numcopies>=num) cout<<"\nCost of "<<num<<" books is Rs. char title[50]={"efg"}.h> class stock { char author[50]. title. void getdata(int). price=500.Q. public: stock(). char pub[50]. }. Whenever a customer wants a book. publisher and stock position.h> #include<string. #include<iostream. "<<(price*num). char title[50]. } int stock::access_title(char a[]) { if(strcmp(title. char pub[50]={"hij"}. void input(). the sales person inputs the title and author and the system searches the list and displays whether it is available or not.

cout<<"\nPrices:". cout<<"\ncopies available:". } getch(). } void main() { clrscr(). stock obj[2]. for(int i=0. for(i=0. cout<<endl. cout<<"\nPublisher:".access_title(ttle)) { cout<<"\nHow many copies? ". cin>>price.i<2. obj[i].input(). cout<<"Enter details of 3 books". } else cout<<"\nBook unavailable". gets(author). cout<<"\n Enter title of required book\n". cout<<"\nAuthor:". gets(pub). char ttle[50].getdata(n). int n.++i) obj[i]. cin>>n. cin>>numcopies.i<2. } .void stock::input() { cout<<"\nTitle: ". gets(title). gets(ttle).i++) { if(obj[i].

16000 .OUTPUT: Enter details of 3 books Title: Da Vinci Code Author:Dan Brown Publisher:Sun Prices:455 copies available:300 Title: Harry Potter Author:J K Rowling Publisher:Bloomsbury Prices:800 copies available:100 Enter title of required book HarryPotter Book available How many copies? 20 Cost of 20 books is Rs.

Data File Handling .

int i. fout. #include<iostream.h> #include<fstream. Berlioz" and output the string one character atra time to the disk file.Berlioz".Write a c++ program.h> #include<string. } OUTPUT: The string has been successfully written into the file . which initialises a string variable to the content. for(i=0.h> #include<conio.open("OUT.ios::out).i<len.TXT". "time is great teacher but unfortunately it kills all its pupils.Q. } cout<<"The string has been successfully written into the file". fout.h> int main() { char str[]="Time is a great teacher but unfortunately it kills all its pupils. ofstream fout.ch.put(ch). int len=strlen(str).i++){ ch=str[i]. return 0.

file. count and display the no. for(i=0. int i.txt".h> void main() { clrscr(). char str[50].ios::beg).ios::in|ios::out|ios::trunc).h> #include<conio.i++) file.h> #include<ctype. of alphabets present in it.Q. gets(str). for(i=0.TXT . file.i++) { if(isalpha(str[i])) ctr++.put(str[i]).open("Story. } OUTPUT: Enter string KARTHIKRAO88.str[i]!='\0'. } cout<<"\nNumber of alphabets is\t"<<ctr. #include<iostream. cout<<"\nEnter string\n". fstream file.Write a user defined function in c++ to read the content from a text file STORY.str[i]!='\0'.seekg(0. getch(). int ctr=0.h> #include<fstream.RAO Number of alphabets is 13 .h> #include<stdio.

Q. cout<<"\nJoke:\n". char type[5]. int n. } void showjoke() { cout<<"\nJoke id:"<<joke_id.i++) { jo. void add_joke() { fstream file. for(i=0.ios::binary|ios::app|ios::in|ios::out). #include<iostream. cin>>n. } file.i<n. cout<<"\nEnter the joke:\n". cin>>joke_id. } . joke jo. cout<<"\nJoke type:".sizeof(jo)). file. public: void newjoke() { cout<<"\nEnter joke id:".h> class joke{ int joke_id. char joke_desc[200]. gets(type).close().h> #include<stdio. file. } }.write((char*)&jo.h> #include<fstream.i. cout<<"\nHow many jokes?".open("joke". puts(joke_desc). gets(joke_desc).Assuming a binary file JOKES.h> #include<conio.TXT is containing objects belonging to a class JOKE.newjoke(). cout<<"\nEnter joke type:(max 4 letters)". cout<<"HAHAHA". puts(type). write user defined function in c++ to add more objects belonging to class JOKE at the bottom of it.

cout<<"HAHAHA".void main() { clrscr(). getch(). jo. add_joke(). } OUTPUT: HAHAHA How many jokes?2 Enter joke id:1 Enter joke type:(max 4 letters)HUMR Enter the joke: ABCDEFGHIJKLM Enter joke id:2 Enter joke type:(max 4 letters)HUMR Enter the joke: QWERTYUIO Joke id:2 Joke type:HUMR Joke: QWERTYUIO HAHAHA .showjoke().

POINTERS .

name[i]=name[j]."sheena"}. getch(). cout<<"\nOriginal string\n".Q.i++) cout<<name[i]<<endl. int i. } OUTPUT: Original string anand naureen banjot wahid sheena Reversed string: sheena wahid banjot naureen anand .j=4.j.h> #include<string.i++) cout<<name[i]<<endl."banjot". char *t."wahid".i<5.h> #include<conio.h> void main() { clrscr(). char *name[]={"anand". for(i=0. } cout<<"\nReversed string:\n". for(i=0.Suppose 7 names are stored in an array of pointers names[] as shown below.i<5/2."naureen".i++. Write a program to reverse the order of these names. for(i=0.i<5. #include<iostream.j--) { t=name[i]. name[j]=t.

The string should get converted into an unrecognizable form. #include<iostream. gets(str).i<l.h> #include<conio.Write a function to encode a string that is passed to it. for(i=0. } cout<<"\nThe proper string is:"<<endl. for(i=0.i<l. } cout<<"\nThe encoded string is:"<<endl.int l) { int i.i++) cout<<str1[i].i<l. for(i=0.int l).h> void encode(char str1[]. cout<<"Enter an string:".i++) str1[i]=str1[i]-10.len).i<l.i++){ str1[i]=str1[i]+10.h> #include<stdio. int main() { char str[30]. int len=strlen(str). encode(str. } void encode(char str1[]. for(i=0.Q.h> #include<string. return 0. } .i++){ cout<<str1[i].

OUTPUT: Enter an string:KARTHIK The encoded string is: UK\^RSU The proper string is: KARTHIK .

return 0.*f. . cin>>ch. Ig\f the given character is not found in the string the function should return NULL. #include<iostream.i++){ if(*x==d) return x.ch). len=strlen(str). else cout<<"Character is not found.h> #include<conio. x++.h> #include<string.".ch. cout<<"Enter a string:".Q. The prototype if the function is char * search(char *. gets(str).char). } char *search(char *x. } return NULL. for(i=0.Write a function search() which scans a string from beginning to end in search of character. if(f!=NULL) cout<<"Character is found. } OUTPUT: Enter a string:KARTHIK Enter the character to be searched for:R Character is found. f=search(str.i<len.h> #include<stdio.char d) { int i.h> char *search(char *. If the character is found it should return a pointer to the first occurence of the given character in the string.char). cout<<"Enter the character to be searched for:". int main() { char str[20].". int len.