You are on page 1of 42

# 2013

C++ programs

INDEX
Sr.No 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. Name

Area and volume of sphere by using compound function Area and volume of sphere Area of triangle by using switch statement Area of triangle Average of n different numbers by using exit control loop Average of n numbers by using function. Bonus and balance Conversion of temperature Cube of a number by using function with argument and no return value. Factorial of a number by using function Factorial of a number by using recursive function. Finding roots of a quadratic equation by using function with argument and without return value. Finding square,cube ,sum of squares cubes in a tabular format. Finding the sum by using recursive function Finding the value of f by considering laminar or turbulent flow. Finding the volume of sphere by using function with argument and no return value. Gets puts function Maximum of three numbers by using function Maximum of three numbers(method 1) Maximum of three numbers(method 2) Sum of given number series. Program of setbase(manipulator function) Program of setfill(manipulator function) Program of setw(manipulator function) Program to perform mathematical operations Program to read the customer number and charges Roots of quadratic equation by using switch statement Program of regration Sum of 1 to 10 numbers except 5 Sum of 1 to 100 odd numbers by using function Sum of three numbers by using function Sum of two numbers Program of setprecision Sum of odd and even numbers from 1 to 100 by using compound function.

AREA AND VOLUME OF SPHERE BY USING COMPUND FUNCTION WITH ARGUMENT AND RETURN VALUE. #include<iostream.h> #include<conio.h> int main() { float area(int r); int r; float z; clrscr(); z=area(r); cout<<"z="<<z; getch(); return(0); } float area(int r) { float volume(int r); float a,v,pie=3.14; cout<<"enter the radius"; cin>>r; a=4*pie*r*r; v=volume(r); cout<<"volume="<<v<<endl; return(a); } float volume(int r) { float volume,pie=3.14; volume=(1.33)*pie*r*r*r; return(volume); } output: enter the radius 3 Volume=112.757401 Z=113.040001 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

AREA AND VOLUME OF SPHERE #include<iostream.h> #include<conio.h> int main() { int r; float pie=3.14,volume,area; clrscr(); cout<<"enter the value of r"; cin>>r; area=4*pie*r*r; volume=(4/3)*pie*r*r*r; cout<<"area="<<area<<endl<<"volume="<<volume; getch(); } output: enter the value of r; 2 area=50.240002 volume=25.120001 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

AREA OF TRIANGLE BY USING SWITCH STATEMENT #include<iostream.h> #include<conio.h> #include<math.h> #include<stdio.h> int main() { int a,b,c,i,base,height; float area,s; clrscr(); cout<<"enter 1 for base & height and 2 for 3 sides"; cin>>i; switch(i) { case 1 : { cout<<"enter the values of base and height"; cin>>base>>height; area=(0.5)*base*height; cout<<"area="<<area; break; } case 2: { cout<<"enter three sides"; cin>>a>>b>>c; s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); cout<<"s="<<s<<endl<<"area="<<area; break; } } getch(); } output: enter 1 for base & height and 2 for 3 sides 1 enter the values of base and height 44 66 area=1452 enter 1 for base & height and 2 for 3 sides 2 enter three sides 44 66 88 s=99 area=1405.892955

## Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

AREA OF TRIANGLE #include<iostream.h> #include<conio.h> #include<math.h> int main() { int a,b,c,s; float area; clrscr(); cout<<"enter the value of a,b and c\n"; cin>>a>>b>>c; s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); cout<<"s="<<s<<endl<<"area="<<area; getch(); } output: enter the value of a,b and c 66 77 55 s=99 area=129.336777 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

AVERAGE OF n DIFFERENT NUMBERS BY USING EXIT CONTROL LOOP #include<iostream.h> #include<conio.h> #include<stdio.h> int main() { int n,x,sum=0,i,average; clrscr(); cout<<"enter the number"; cin>>n; for(i=0;i<n;i++) { cout<<"x="; cin>>x; sum=sum+x; } cout<<"sum="<<sum; average=sum/n; cout<<"average="<<average; getch(); } output: enter the number 2 x=4 x=8 sum=12 average=6 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

AVERAGE OF n NUMBERS BY USING FUNCTION WITH ARGUMENT AND RETURN VALUE. #include<iostream.h> #include<conio.h> int main() { float avg(int n); int n,z; clrscr(); cout<<"enter the n"; cin>>n; z=avg(n); cout<<"z="<<z; getch(); return(0); } float avg(int n) { int x,i,sum=0; float avg; for(i=1;i<=n;i++) { cout<<"enter x="; cin>>x; sum=sum+x; } avg=sum/n; return(avg); } output: enter the n 3 enter x=1 enter x=2 enter x=3 z=2 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

BONUS AND BALANCE #include<iostream.h> #include<conio.h> #include<stdio.h> int main() { int i; float bonus,balance; clrscr(); cout<<"enter 1 for male and 2 for female"; cin>>i; cout<<"enter the balance"; cin>>balance; switch(i) { case 1: { if(balance>20000) { bonus=0.05*balance; cout<<"bonus="<<bonus; } break; } case 2: { bonus=0.02*balance; cout<<"bonus="<<bonus; break; } } balance=balance+bonus; cout<<endl<<"balance="<<balance; getch(); } output: enter 1 for male and 2 for female 1 enter the balance 30000 bonus=1500 balance=31500 enter 1 for male and 2 for female 2 enter the balance 40000 bonus=800 balance=40800 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

CONVERSION OF TEMPERATURE #include<iostream.h> #include<conio.h> int main() { int i; float c,f; cout<<"enter 1 for c and 2 for f"; cin>>i; switch(i) { case 1: { cout<<"enter the value of f"; cin>>f; c=((32-f)/1.8); cout<<"c="<<c; break; } case 2: { cout<<"enter the value of c"; cin>>c; f=((1.8*c)+32); cout<<"f="<<f; break; } } getch(); } output: enter 1 for c and 2 for f 1 enter the value of f 40.6 c=-4.777777 enter 1 for c and 2 for f 2 enter the value of c 56 f=132.800003 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

CUBE OF A NUMBER BY USING FUNCTION WITH ARGUMENT AND NO RETURN VALUE. #include<iostream.h> #include<conio.h> int main() { void cube(int a); int a; clrscr(); cout<<"enter the number"; cin>>a; cube(a); return(0); getch(); } void cube(int a) { int c; c=a*a*a; { cout<<"cube="<<c; } getch(); } output: enter the number 3 cube=27 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

FACTORIAL OF A NUMBER BY USING FUNCTION WITH ARGUMENT AND RETURN VALUE. #include<iostream.h> #include<conio.h> #include<stdio.h> int main() { int fact(int n); int n,c; clrscr(); cout<<"enter number"; cin>>n; c=fact(n); cout<<"factorial="<<c; getch(); return(0); } int fact(int n) { int i,fact=1; for(i=1;i<=n;i++) { fact=fact*i; } return(fact); } output: enter number 4 Factorial=24 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

FACTORIAL OF A NUMBER BY USING RECURSIVE FUNCTION. #include<iostream.h> #include<conio.h> int main() { int fact(int x); int n,b,x; clrscr(); cout<<"enter the number"; cin>>n; b=fact(n); cout<<"b="<<b; cin>>b; getch(); return(0); } int fact(int n) { int f; if(n==1) return(1); else f=n*fact(n-1); return(f); } output: enter the number 3 b=6 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

FINDING ROOTS OF A QUADRATIC EQUATION BY USING FUNCTION WITH ARGUMENT AND WITHOUT RETURN VALUE. #include<iostream.h> #include<conio.h> #include<math.h> int main() { void roots(int a,int b,int c); int a,b,c; float disc; clrscr(); cout<<"enter the value of a,b and c"; cin>>a>>b>>c; roots(a,b,c); getch(); return(0); } void roots(int a,int b,int c) { int k,disc; float root1,root2; disc=(b*b)-(4*a*c); if(disc<0)k=1; else if(disc==0)k=2; else if(disc>0)k=3; switch(k) { case 1: { cout<<"roots are imaginary"; break; } case 2: { cout<<"roots are real and equal"; root1=root2=-b/(2*a); cout<<"root1=root2="<<root1<<root2; break; } case 3: { root1=(-b-sqrt(disc))/(2*a); root2=(-b+sqrt(disc))/(2*a); cout<<"root1="<<root1<<"root2="<<root2; break; } } getch(); } output: enter the values of a,b and c 1

2 3 roots are imaginary enter the value of a,b and c 1 4 4 roots are real and equal root1=root2=-2-2 enter the value of a,b and c 2 6 4 root1=-2 root2=-1 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

FINDING SQUARE,CUBE,SUM OF SQUARES AND SUM OF CUBE IN A TABULAR FORMAT #include<iostream.h> #include<conio.h> #include<stdio.h> int main() { int num=0,square,cube,squaresum=0,cubesum=0,i=0; clrscr(); cout<<"num"<<'\t'<<"square"<<'\t'<<"cube"; for(i=0;i<=10;i++) { //cout<<"\n"<<i<<"\t"<<square<<'\t'<<cube; num=num+i; square=i*i; squaresum=squaresum+square; cube=square*i; cubesum=cubesum+cube; cout<<"\n"<<i<<"\t"<<square<<'\t'<<cube; } cout<<"\n"; cout<<"\n"<<""<<num<<"\t"<<""<<squaresum<<"\t"<<""<<cubesum; getch(); } output: num 0 1 2 3 4 5 6 7 8 9 10 55

## Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

FINDING THE SUM BY USING RECURSIVE FUNCTION #include<iostream.h> #include<conio.h> int main() { int sum(int n ); int n,a; clrscr(); cout<<"enter the number"; cin>>n; a=sum(n); cout<<"a="<<a; getch(); return(0); } int sum(int n) { int x,i,sum=0; if(x==1) return(1); for(i=0;i<=n;i++) { sum=sum+i; } return(sum); } output: enter the number 4 a=10 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

FINDING THE VALUE OF FRICTION FACTOR BY CONSIDERING LAMINAR OR TURBULENT FLOW. #include<iostream.h> #include<conio.h> #include<math.h> int main() { int d,dens; float f,Nre,visc,v; clrscr(); cout<<"enter the value of d,v,dens and visc"; cin>>d>>v>>dens>>visc; Nre=(d*v*dens)/visc; { cout<<endl<<"Nre="<<Nre; } if(Nre<2100) { cout<<endl<<"the flow is laminar"; f=(16)/(Nre); cout<<endl<<"f="<<f; } else if(Nre>4000) { cout<<endl<<"the flow is turbulent"; f=(0.078)/(Nre)*(0.25); cout<<endl<<"f="<<f; } getch(); } output: enter the value of d,v,dens and visc 1 0.25 50 0.001 Nre=12499.999023 the flow is turbulent f=1.56e-0.6 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

FINDING THE VOLUME OF SPHERE BY USING FUNCTION WITH ARGUMENT AND NO RETURN VALUE. #include<iostream.h> #include<stdio.h> #include<conio.h> int main() { void volume(int r); int r; clrscr(); cout<<"enter the value of r"; cin>>r; volume(r); return(0); getch(); } void volume(int r) { float volume,pie=3.14; volume=(1.33)*(pie)*r*r*r; cout<<"volume="<<volume; getch(); } output: enter the value of r 2 volume=33.409599 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

GETS PUTS FUNCTION #include<iostream.h> #include<stdio.h> #include<conio.h> int main() { char name[50]; clrscr(); cout<<"enter your name"<<endl; gets(name); cout<<"entered name"<<endl; puts(name); getch(); } output: enter your name gokul entered name gokul Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

MAXIMUM OF THREE NUMBERS BY USING FUNCTION WITH ARGUMENT AND RETURN VALUE. #include<iostream.h> #include<conio.h> int main() { int max(int a,int b,int c); int a,b,c,z; clrscr(); cout<<"enter the value of a,b & c"; cin>>a>>b>>c; z=max(a,b,c); cout<<"z="<<z<<endl; getch(); return(0); } int max(int a,int b,int c) { int max; max=a; if(b>max)max=b; if(c>max)max=c; return(max); } output: enter the value of a,b and c 22 33 11 z=33 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

MAXIMUM OF THREE NUMBERS(method 1) #include<iostream.h> #include<conio.h> int main() { int a,b,c,max; clrscr(); cout<<"enter the value of a,b and c"; cin>>a>>b>>c; max=a; if(b>max)max=b; if(c>max)max=c; cout<<"max="<<max; getch(); } output: enter the value of a,b and c 24 25 23 max=25 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

MAXIMUM OF THREE NUMBERS(method 2) #include<iostream.h> #include<conio.h> int main() { int a,b,c,max; cout<<"enter the value of a,b and c"; cin>>a>>b>>c; if(a>b && a>c) { cout<<"a is greater"; } else if(b>c) { cout<<"b is greater"; } else { cout<<"c is greater"; } getch(); } output: enter the value of a,b and c 4 5 6 c is greater Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

WRITE A PROGRAM WHICH READS THE ALL POSITIVE INTEGER N AND CALCULATE THE SUM 1+(1/2)+(1/3)+(1/4)+-------(1/N). #include<iostream.h> #include<conio.h> int main() { int n,i; float sum=0; clrscr(); cout<<"enter an odd integer"; cin>>n; for(i=1;i<=n;i++) { sum=sum+(1/float(i)); } cout<<"sum="<<sum; getch(); } output: enter an odd integer 5 Sum=2.283334 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

PROGRAM OF SETBASE (MANIPULATOR FUNCTION) #include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { int n; clrscr(); cout<<"enter the number"<<endl; cin>>n; cout<<"decimal base="<<dec<<n<<endl; cout<<"hexadecimal base="<<hex<<n<<endl; cout<<"octal base="<<oct<<n<<endl; getch(); } output: enter the number 14 decimal base=14 hexadecimal base=e octal base=16 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

PROGRAM OF SETFILL(MANIPULATOR FUNCTION) #include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { int a=100,b=200; clrscr(); cout<<setfill('*'); cout<<setw(5)<<a<<setw(5)<<b<<endl; cout<<setw(6)<<a<<setw(6)<<b<<endl; cout<<setw(7)<<a<<setw(7)<<b<<endl; cout<<setw(8)<<a<<setw(8)<<b<<endl; getch(); } output: ****100****200 *****100*****200 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

PROGRAM OF SETW(MANIPULATOR FUNCTION) #include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { int a=100,b=200; clrscr(); cout<<a<<setw(5)<<b<<endl; cout<<a<<setw(6)<<b<<endl; getch(); } output: 100 200 100 200 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

PROGRAM TO PERFORM MATHEMATICAL OPERATIONS #include<iostream.h> #include<conio.h> #include<stdio.h> int main() { int add,sub,multi,div,x,y,i; clrscr(); cout<<"enter 1 for addition,2 for substraction,3 for multiplication and 4 for division"; cin>>i; cout<<"enter the value of x and y"; cin>>x>>y; switch(i) { case 1: { add=x+y; cout<<"add="<<add; break; } case 2: { if(x>y) { sub=x-y; cout<<"sub="<<sub; } else { sub=y-x; cout<<"sub="<<sub; } break; } case 3: { multi=x*y; cout<<"multi="<<multi; break; } case 4: { div=(x/y); cout<<"div="<<div; break; } } getch(); } output:

enter 1 for addition,2 for substraction,3 for multiplication and 4 for division 1 enter the value of x and y 2 3 add=5 enter 1 for addition,2 for substraction,3 for multiplication and 4 for division 2 enter the value of x and y 33 44 sub=11 enter 1 for addition,2 for substraction,3 for multiplication and 4 for division 3 enter the value of x and y 2 3 multi=6 enter 1 for addition,2 for substraction,3 for multiplication and 4 for division 4 enter the value of x and y 10 5 div=2 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

PROGRAM TO READ THE CUSTOMER NUMBER AND CHARGES #include<iostream.h> #include<conio.h> int main() { int custnum,units; float charges; cout<<"enter custnum and units"; cin>>custnum>>units; if(units<=200) { charges=1.0*units; } else if(units<=400) { charges=200+1.30*(units-200); } else if(units<=600) { charges=460+1.60*(units-400); } else { charges=780+2.0*(units-600); } cout<<"custnum="<<custnum<<endl<<"charges="<<charges; getch(); } output: enter custnum and units 1 20 custnum=1 charges=20.00 enter custnum and units 1 220 custnum=1 charges=226 enter custnum and units 1 420 custnum=1 charges=492 enter custnum and units 1 720 custnum=1

## charges=1020 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

ROOTS OF QUADRATIC EQUATION BY USING SWITCH STATEMENT #include<iostream.h> #include<conio.h> #include<math.h> int main() { int a,b,c,k; float disc,root1,root2; clrscr(); cout<<"enter the value of a,b and c"; cin>>a>>b>>c; disc=(b*b)-(4*a*c); if(disc<0)k=1; else if(disc==0)k=2; else if(disc>0)k=3; switch(k) { case 1: { cout<<"roots are imaginary"; break; } case 2: { cout<<"roots are real and equal"; root1=root2=-b/(2*a); cout<<"root1=root2="<<root1<<root2; break; } case 3: { root1=(-b-sqrt(disc))/(2*a); root2=(-b+sqrt(disc))/(2*a); cout<<"root1="<<root1<<"root2="<<root2; break; } } getch(); } output: enter the value of a,b and c 1 2 3 root are imaginary enter the value of a,b and c 1 4 4 roots are real and equal root1=root2=-2-2

enter the value of a,b and c 2 6 4 root1-2 root2=-1 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

PROGRAM THAT READS A GIVEN NUMBER OF PAIRS (X,Y) OF REAL NUMBERS AND THEN COMPUTE THE LEAST SQUARE REGRATION LINE FOR THE DATA SET.USE THE EQUATION Y=mx+b m=(sumxy-(meany*sumx))/(sumxx-(meanx*sumx)). #include<iostream.h> #include<conio.h> int main() { int n,i; float x,y,sumx=0,sumy=0,sumxy=0,m,b; float meanx,meany,meanxx,sumxx; clrscr(); cout<<"How many points?"; cin>>n; for(i=0;i<=n;i++) { cout<<"x="; cin>>x; cout<<"y="; cin>>y; sumx=sumx+x; sumy=sumy+y; sumxx=sumxx+x*x; sumxy=sumxy+x*y; meany=sumy/n; meanx=sumx/n; } m=(sumxy-(meany*sumx))/(sumxx-(meanx*sumx)); b=meany-(m*meanx); cout<<"y="<<m<<"x+"<<b; getch(); return(0); } Output: How many points? 1 x=77 y=66 x=55 y=33 y=0.728571x+2.828573 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

SUM OF 1 TO 10 NUMBERS EXCEPT 5 #include<iostream.h> #include<conio.h> int main() { int sum=0,i; for(i=0;i<=10;i++) { if(i==5) goto end; sum=sum+i; end: } cout<<"sum="<<sum; getch(); } output: sum=50 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

SUM OF 1 TO 100 ODD NUMBERS BY USING FUNCTION WITHOUT ARGUMENT AND WITH RETURN VALUE.

#include<iostream.h> #include<conio.h> #include<stdio.h> int main() { int osum(); int z; clrscr(); z=osum(); cout<<"z="<<z; getch(); return(0); } int osum() { int i,osum=0; for(i=1;i<=100;i+2) { osum=osum+i; } return(osum); } output: z=2500 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

SUM OF ODD AND EVEN NUMBERS FROM 1 TO 100 BY USING COMPOUND FUNCTION. #include<iostream.h> #include<conio.h> #include<stdio.h> int main() { clrscr(); void even(); void odd(); even(); odd(); getch(); return(0); } void even() { int i,sum=0; for(i=0;i<=100;i+2) { sum=sum+i; } cout<<"sum of even numbers="<<sum<<endl; } void odd() { int j,sum1=0; for(j=1;j<=100;j+2) { sum1=sum1+j; } cout<<"sum of odd numbers="<<sum1<<endl; } output: sum of even numbers=2550 sum of odd numbers=5050 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

SUM OF THREE NUMBERS BY USING FUNCTION WITH ARGUMENT AND RETURN VALUE. #include<iostream.h> #include<conio.h> void main() { int sum(int a,int b,int c); int a,b,c,z; clrscr(); cout<<"enter the value of a,b and c"; cin>>a>>b>>c; z=sum(a,b,c); cout<<"z="<<z; getch(); } int sum(int a,int b,int c) { int z,sum=0; z=a+b+c; return(z); } output: enter the value of a,b and c 1 2 3 z=6 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

SUM OF TWO NUMBERS #include<iostream.h> #include<conio.h> int main() { int a,b,sum; clrscr(); cout<<"enter the value of a and b"; cin>>a>>b; sum=a+b; cout<<"sum="<<sum; getch(); } output: enter the value a and b 12 12 sum=24 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.

WRITE A PROGRAM THAT CALCULATE RATIO OF GIVEN ANY TWO NUMBERS & CONTROL OVER THE 3 DIGITS AFTER THE DECIMAL POINT OF THE OUTPUT #include<iostream.h> #include<conio.h> #include<iomanip.h> int main() { float a,b,c; clrscr(); a=5; b=3; c=a/b; cout<<setprecision(5)<<c<<endl; getch(); } output: 1.66667 Name:Gokul Krishna Kadam T.E Chemical,TKIET,Warananagar.