revise c++

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

Upload: amit-tamboli

Post on 08-Nov-2014

47 views

Category:

Documents


1 download

DESCRIPTION

asd

TRANSCRIPT

Page 1: Revise C++

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

Page 2: Revise C++

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

Page 3: Revise C++

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:

Page 4: Revise C++

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

Page 5: Revise C++

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

Page 6: Revise C++

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

Page 7: Revise C++

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

Page 8: Revise C++

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

Page 9: Revise C++

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

Page 10: Revise C++

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

Page 11: Revise C++

12 14 15 16 17 18 19 10 13 23 34

Page 12: Revise C++

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

Page 13: Revise C++

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 - (U^2) + 1/2!(U^2) -1/3!(U^3) + ............. #include<iostream.h> #include<conio.h> #include<math.h> int main() { int x,p,i,j; double fact=1.0,ans=1; cout<<"Enter the value of x:"; cin>>x; cout<<"Enter till what power you want:"; cin>>p; for(i=1;i<=p;i++){ fact=fact*i; ans+=(pow(-1,i))*((pow(x,i))/(fact)); } cout<<"The sum of the series is:"<<ans; return 0; } OUTPUT: Enter the value of x:3 Enter till what power you want:4 The sum of the series is:1.375

Page 14: Revise C++

Structures

Page 15: Revise C++

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

Page 16: Revise C++

void spouse::display() { person::display(); cout<<"Spouse name\t";puts(spousename); } void main() { clrscr(); spouse obj; obj.getsp(); obj.display(); getch(); } 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 ******************

Page 17: Revise C++

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

Page 18: Revise C++

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

Page 19: Revise C++

puts(pl2[plno].name); cout<<"Runs given by the player is :"<<pl2[plno].rnsgvn<<endl; cout<<"Total overs played by the player :"<<pl2[plno].ttvrs<<endl; cout<<"Total wickets taken by the user :"<<pl2[plno].wktstkn<<endl; break; default: cout<<"Idiot enter a decent value"<<endl; goto abc; } cout<<endl<<endl<<endl<<"Do you wish to continue? Y-1 N-2"<<endl; cin>>plno; if (plno==1) goto xyz; else cout<<"Thank you Press any key to exit"; getch(); } 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

Page 20: Revise C++

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

Page 21: Revise C++

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

Page 22: Revise C++

Object Oriented Programming

Page 23: Revise C++

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

Page 24: Revise C++

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

Page 25: Revise C++

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

Page 26: Revise C++

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

Page 27: Revise C++

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

Page 28: Revise C++

Classes And Objects

Page 29: Revise C++

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

Page 30: Revise C++

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

Page 31: Revise C++

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

Page 32: Revise C++

taxpayer a; a.inputdata(); clrscr(); a.computetax(); a.display(); getch(); } 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

Page 33: Revise C++

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

Page 34: Revise C++

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

Page 35: Revise C++

void student::displaymarks() { cout<<"Roll no:"<<roll_no<<endl; cout<<"Name:"<<name<<endl; cout<<"Class:"<<class_st<<endl; cout<<"Percentage:"<<calculate()<<endl; } int main() { student s1; s1.readmarks(); s1.displaymarks(); return 0; } OUTPUT: Enter the roll no.: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.599998

Page 36: Revise C++

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

Page 37: Revise C++

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

Page 38: Revise C++

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:50 Data of serial 2 is: Serial code is:122 Title is:solomon Durations is:60 No. of episodes are:20

Page 39: Revise C++

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

Page 40: Revise C++

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

Page 41: Revise C++

c1.clear(); cout<<"Do you want to continue:"; cin>>ch; } return 0; } 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

Page 42: Revise C++

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

Page 43: Revise C++

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

Page 44: Revise C++

1.Increment person and total 2.Display no. of people and the amount collected till now 3.Display no. of people who entered 4.Exit Enter your choice:1 The no. of people and the total have beem incremented 1.Increment person and total 2.Display no. of people and the amount collected till now 3.Display no. of people who entered 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.Increment person and total 2.Display no. of people and the amount collected till now 3.Display no. of people who entered 4.Exit Enter your choice:3 The no. of people who have visited so far are:2 1.Increment person and total 2.Display no. of people and the amount collected till now 3.Display no. of people who entered 4.Exit Enter your choice:4

Page 45: Revise C++

Constructors And Destructors

Page 46: Revise C++

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

Page 47: Revise C++

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

Page 48: Revise C++

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. 16000

Page 49: Revise C++

Data File Handling

Page 50: Revise C++

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

Page 51: Revise C++

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

Page 52: Revise C++

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

Page 53: Revise C++

void main() { clrscr(); cout<<"HAHAHA"; add_joke(); jo.showjoke(); getch(); } 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

Page 54: Revise C++

POINTERS

Page 55: Revise C++

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

Page 56: Revise C++

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

Page 57: Revise C++

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

Page 58: Revise C++

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