Вы находитесь на странице: 1из 18

1. C++ Program to output an integer, a floating point number and a character #include <iostream.h> #include <conio.

h> void main() { clrscr(); int x = 10; float y = 10.1; char z = 'a'; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; getch(); } 2. C++ Program to find the sum, difference, product and quotient of two integers #include <iostream.h> #include <conio.h> void main() { clrscr(); int x = 10; int y = 2; int sum, difference, product, quotient; sum = x + y; difference = x - y; product = x * y; quotient = x / y; cout << "The sum of " << x << " & " << y << " is " << sum << "." << endl; cout << "The difference of " << x << " & " << "y << is " << difference << "." << endl; cout << "The product of " << x << " & " << y << " is " << product << "." << endl; cout << "The quotient of " << x << " & " << y << " is " << quotient << "." << endl; getch(); } 3. Program to enter two integers and find their sum and average #include <iostream.h> #include <iostream.h> #include <conio.h> void main() { clrscr(); int x,y,sum; float average; cout << "Enter 2 integers : " << endl; cin>>x>>y; sum=x+y;

average=sum/2; cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl; cout << "The average of " << x << " and " << y << " is " << average << "." << endl; getch(); }

4. Program to enter velocity, acceleration and time and print final velocity using the formula : v = u + a * t
#include <iostream.h> #include <conio.h> void main() { clrscr(); int v,u,a,t; cout << "Enter the velocity, acceleration, time as integers : " << endl; cin>>u>>a>>t; v=u+a*t; cout << "The final velocity is " << v << "." << endl; getch(); }

5. Program to enter your age and print if you should be in grade 10

#include <iostream.h> #include <conio.h> void main() { clrscr(); int age; cout << "Enter your present age : cin>>age; if(age==16) { cout << "Your present age is " << cout << "You are of the right age } else { cout << "Your present age is " << cout << "You are not of the right } getch(); } #include <iostream.h> #include <conio.h>

" << endl;

age << " years." << endl; for joining grade 10 !" << endl;

age << " years." << endl; age for joining grade 10 !" << endl;

6. Program to enter an integer and print if it is greater or less than 100

void main(){ clrscr(); int x; cout << "Enter an integer : " << endl; cin>>x;

if(x>100) { cout << x << " is greater than 100." << endl; } else { cout << x << " is less than 100." << endl; } getch(); }

7. Program to enter the sale value and print the agent's commission
#include <iostream.h> #include <conio.h> void main() { clrscr(); long int svalue; float commission; cout << "Enter the total sale value : " << endl; cin>>svalue; if(svalue<=10000) { commission=svalue*5/100; cout << "For a total sale value of \$" << svalue << ", "; cout << "the agent's commission is \$" << commission; } else if(svalue<=25000) { commission=svalue*10/100; cout << "For a total sale value of \$" << svalue << ", "; cout << "the agent's commission is \$" << commission; } else if(svalue>25000) { commission=svalue*20/100; cout << "For a total sale value of \$" << svalue << ", "; cout << "the agent's commission is \$" << commission; } getch(); } 8. Program to switch between different cases #include <iostream.h> #include <conio.h> int main() { clrscr(); int choice; cout << "1. Talk" << endl; cout << "2. Eat" << endl; cout << "3. Play" << endl; cout << "4. Sleep" << endl; cout << "Enter your choice : " << endl;

cin>>choice; switch(choice) { case 1 : cout << "You chose to << endl; break; case 2 : cout << "You chose to << endl; break; case 3 : cout << "You chose to bad." << endl; break; case 4 : cout << "You chose to habit." << endl; break; default : cout << "You did not << endl; } getch(); } #include <iostream.h> #include <conio.h>

talk...talking too much is a bad habit." eat...eating healthy foodstuff is good." play...playing too much everyday is sleep...sleeping enough is a good choose anything...so exit this program."

9. Program to enter the principal, rate & time and print the simple interest

void main() { clrscr(); int x; float sinterest,principal,rate,time; for(x=4;x>=0;x--) { cout << "Enter the principal, rate & time : " << endl; cin>>principal>>rate>>time; sinterest=(principal*rate*time)/100; cout << "Principal = \$" << principal << endl; cout << "Rate = " << rate << "%" << endl; cout << "Time = " << time << " years" << endl; cout << "Simple Interest = \$" << sinterest << endl; } getch(); }

10. Program to enter an integer and print if it is prime or composite

#include <iostream.h> #include <conio.h> #include <process.h> void main() { clrscr(); int num1,x; cout << "Enter an integer : " << endl; cin>>num1; for(x=2;x<num1;x++) { if(num1%x==0)

{ cout << num1 << " is a composite number." << endl; getch(); exit(0); } else { cout << num1 << " is a prime number." << endl; getch(); exit(0); } } }

11. Program to enter a string and find its length

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

void main() { clrscr(); int slength; char x[81]; //Allowing the user to input a maximum of 80 characters. cout << "Enter the string : " << endl; cin>>x; slength=strlen(x); cout << "The length of the string " << x << " is " << slength << "." << endl; getch(); }

12. Program to enter an integer and output the cube of that integer
#include <iostream.h> #include <conio.h> int cube(int x); //The prototype. void main() { clrscr(); int a; cout << "Enter an integer : "; cin>>a; cout << "The cube of " << a << " is : " << cube(a) << endl; //Call the function cube(a). getch(); } //Defining the function. int cube(int x) { int y; y=x*x*x; return(y); }

13. Program to enter an integer and print out its successor

#include <iostream.h>

#include <conio.h> void value(int); void main() { clrscr(); int x; cout << "Enter an integer : "; cin>>x; cout << "The successor of " << x << " is "; value(x); getch(); } void value(int x) { x++; cout << x << "." << endl; } 14 Program to draw 2 rectangles and fill 1 of them #include #include #include #include #include #include <iostream.h> <conio.h> <graphics.h> <ctype.h> <stdlib.h> <stdio.h>

void main() { clrscr(); int gd = DETECT,gm,errorcode; //Requesting auto-detection. //Initializing graphics and local variables. initgraph (&gd, &gm, "d:\\bc3\\bgi"); //Path where graphics drivers are installed //Read result of initialization. errorcode = graphresult(); //An error occured. if (errorcode != grOk) { cout << "Graphics error occured : \n" << grapherrormsg(errorcode) << endl; cout << "Press any key to stop : "; getch(); exit(1); } /*Drawing a rectangle having top LHS vertex at (300, 300) and bottom RHS vertex at (600, 400)*/ rectangle(300, 300, 600, 400); rectangle(100, 100, 200, 200); getch(); floodfill(120, 120, WHITE); getch(); closegraph();

#include #include #include #include #include #include

void main() { clrscr(); int gd=DETECT,gm,errorcode; //Requesting auto-detection. //Initializing graphics and local variables. initgraph(&gd,&gm,"d:\\bc3\\bgi"); //Path where graphics drivers are installed //Reading result of initialization. errorcode=graphresult(); //An error occured. if (errorcode!=grOk) { cout << "Graphics error occured : \n" << grapherrormsg(errorcode) << endl; cout << "Press any key to stop : "; getch(); exit(1); } circle(200,200,50); //Drawing a circle having center(200,200) and radius(50). getch(); circle(300,203,40); //Drawing a circle having center(300,203) and radius(40). getch(); circle(500,303,80); //Drawing a circle having center(500,303) and radius(80). getch(); closegraph(); }

16. Program to change the background colors on the screen

#include #include #include #include <conio.h> <graphics.h> <stdlib.h> <stdio.h>

void main (int) { int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection. int midx,midy,x; //Initializing graphics and local variables. initgraph(&gdriver,&gmode,"d:\\bc3\\bgi"); //Reading result of initialization. errorcode=graphresult(); if(errorcode!=grOk) //An error occurred.

{ printf("Graphics error occurred : %s \n",grapherrormsg(errorcode)); printf("Press any key to stop : "); getch(); exit(1); //Terminate the program due to error. } /*Changing the background color. Note : Press enter to see the first screen as it is black and it may appear as if the program has stopped running.*/ for(x=0;x<=15;x++) { setbkcolor(x); getch(); } closegraph(); }

17. Program to change the foreground colors and draw circles on the screen
#include #include #include #include <conio.h> <graphics.h> <stdlib.h> <stdio.h>

void main (int) { int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection. int midx,midy,x; //Initializing graphics and local variables. initgraph(&gdriver,&gmode,"d:\\bc3\\bgi"); //Reading result of initialization. errorcode=graphresult(); if(errorcode!=grOk) //An error occurred. { printf("Graphics error occurred : %s \n",grapherrormsg(errorcode)); printf("Press any key to stop : "); getch(); exit(1); //Terminate the program due to error. } /*Changing the foreground color. Note : Press enter to exit the last screen as it is black and it may appear as if the program has stopped running.*/ for(x=15;x>=0;x--) { setcolor(x); circle(20+(x*40),200,15);/*Changing x-coordinate by 50 each time so that the circles do not overlap.*/ getch(); } cleardevice(); //Clearing the screen in the graphics mode. circle(200,200,50); getch(); closegraph(); }

18. Program to write in different fonts on the screen

#include <conio.h>

#include <graphics.h> #include <stdlib.h> #include <stdio.h> void main (int) { int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection. int midx,midy,fstyle; //Initializing graphics and local variables. initgraph(&gdriver,&gmode,"d:\\bc3\\bgi"); //Reading result of initialization. errorcode=graphresult(); if(errorcode!=grOk) //An error occurred. { printf("Graphics error occurred : %s \n",grapherrormsg(errorcode)); printf("Press any key to stop : "); getch(); exit(1); //Terminate the program due to error. } //Changing the font styles using a loop. cleardevice(); settextstyle(DEFAULT_FONT,HORIZ_DIR,4); /*The above statement means that it is the default font in the horizontal direction and the font size is 4.*/ //Outputting a message. outtextxy(200,200,"Default font"); getch(); cleardevice(); settextstyle(TRIPLEX_FONT,VERT_DIR,5); /*The above statement means that it is the triplex font in the vertical direction and the font size is 5.*/ //Outputting a message. outtextxy(200,200,"Triplex font"); getch(); cleardevice(); settextstyle(GOTHIC_FONT,HORIZ_DIR,5); /*The above statement means that it is the default font in the horizontal direction and the font size is 2.*/ //Outputting a message. outtextxy(200,200,"Gothic font"); getch(); cleardevice(); settextstyle(SMALL_FONT,VERT_DIR,5); /*The above statement means that it is the small font in the vertical direction and the font size is 5.*/ //Outputting a message. outtextxy(200,200,"Small font"); getch(); cleardevice(); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,5); /*The above statement means that it is the sans serif font in the horizontal direction and the font size is 5.*/ //Outputting a message.

19. Program to construct a 3-dimensional bar

#include #include #include #include <conio.h> <graphics.h> <stdlib.h> <stdio.h>

void main (int) { int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection. int midx,midy,x; //Initializing graphics and local variables. initgraph(&gdriver,&gmode,"d:\\bc3\\bgi"); //Reading result of initialization. errorcode=graphresult(); if(errorcode!=grOk) //An error occurred. { printf("Graphics error occurred : %s \n",grapherrormsg(errorcode)); printf("Press any key to stop : "); getch(); exit(1); //Terminate the program due to error. } setfillstyle(EMPTY_FILL,0); /*The above statement means that the setfillstyle function is used to set the fill style of the 3-d bar as a blank.*/ bar3d(200,200,300,450,10,1); getch(); closegraph(); }

20. Program to plot pixels

#include #include #include #include

<conio.h> <graphics.h> <stdlib.h> <stdio.h>

void main (int) { int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection. int midx,midy,x; //Initializing graphics and local variables. initgraph(&gdriver,&gmode,"d:\\bc3\\bgi"); //Reading result of initialization. errorcode=graphresult(); if(errorcode!=grOk) //An error occurred. { printf("Graphics error occurred : %s \n",grapherrormsg(errorcode)); printf("Press any key to stop : "); getch();

exit(1); //Terminate the program due to error. } for(x=40;x<120;x=x+10) { putpixel(x,200,14); //Plots 1 pixel at position(x,200) with YELLOW color(14) getch(); }

} 21. Program to enter two integers and print the quotient and remainder
#include <iostream.h> #include <conio.h> int main() { clrscr(); int x,y,quotient,remainder; cout << "Enter 2 integers greater than 0 : "; cin>>x>>y; quotient=x/y; remainder=x-(quotient*y); cout << "Quotient of " << x << " & " << y << " = " << quotient << "\n"; cout << "Remainder" << " = " << remainder << "\n"; getch(); return 0; }

22. Program to enter an integer and output its 15 multiples

#include <iostream.h> #include <conio.h> int main() { clrscr(); int x; cout << "Enter an integer less than 2185 : "; cin>>x; cout << "The first 15 multiples of " << x << " are : "; for(int y=1;y<16;y++) cout << "\n" << x << "x" << y << "=" << x*y; getch(); return 0; }

23. Program to enter three integers and output the biggest integer

#include <iostream.h> #include <conio.h> int main() { clrscr(); int x,y,z,biggest; cout << "Enter 3 integers : "; cin>>x>>y>>z; biggest=x>y?(x>z?x:z):(y>z?y:z); cout << "The biggest integer out of the 3 integers you typed "; cout << x << ", " << y << " & " << z << " is : " << "\n" << biggest << "\n"; getch(); return 0;

24. Program to enter an integer and find out if it is even or odd

#include <iostream.h> #include <conio.h> void main() { clrscr(); int x; cout << "Enter an integer : "; cin>>x; if(x%2==0) cout << "The number " << x << " is even."; else cout << "The number " << x << " is odd."; getch();

} 25. Program to enter the unit reading and output the customer's telephone bill
#include <iostream.h> #include <conio.h> int main() { clrscr(); long int units,charge=0; float total; const int rent=25; cout << "Enter the number of units used : "; cin>>units; if(units>200) charge=(units-200)*20+150*40+50*60; else if(units>50) charge=(units-50)*40+50*60; else charge=units*60; total=0.01*charge+rent; cout << "You have used " << units << " units." << endl; cout << "Your total telephone bill is \$" << total; getch(); return 0; }

26. Program to count the number of words and characters in a sentence

#include <iostream.h> #include <conio.h> void main() { clrscr(); int countch=0; int countwd=1; cout << "Enter your sentence in lowercase: " << endl; char ch='a'; while(ch!='\r') { ch=getche(); if(ch==' ') countwd++;

else countch++; } cout << "\n Words = " << countwd << endl; cout << "Characters = " << countch-1 << endl; getch(); }

27. Program to enter salary and output income tax and net salary
#include <iostream.h> #include <conio.h>

void main() { clrscr(); int itrate; float salary,itax,nsalary=0; cout << "Enter the salary : "; cin>>salary; if(salary>15000) { itax=salary*30/100; itrate=30; } else if(salary>=7000) { itax=salary*20/100; itrate=20; } else { itax=salary*10/100; itrate=10; } nsalary=salary-itax; cout << "Salary = \$" << salary << endl; cout << "Your income tax @ " << itrate << "% = \$" << itax << endl; cout << "Your net salary = \$" << nsalary << endl; getch();

28. Program to find the roots of a quadratic equation

#include <iostream.h> #include <conio.h> #include <math.h> int main() { clrscr(); float a,b,c,d,root1,root2; cout << "Enter the 3 coefficients a, b, c : " << endl; cin>>a>>b>>c; if(!a){ if(!b)

cout << "Both a and b cannot be 0 in ax^2 + bx + c = 0" << "\n"; else { d=-c/b; cout << "The solution of the linear equation is : " << d << endl; } } else { d=b*b-4*a*c; if(d>0) root1=(-b+sqrt(d))/(2*a); root2=(-b-sqrt(d))/(2*a); cout << "The first root = " << root1 << endl; cout << "The second root = " << root2 << endl; } getch(); return 0; }

29. Program to enter an integer and output it in the reversed form

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

void main() { clrscr(); long int num1,num2,rnum=0; cout << "Enter an integer : " << endl; cin>>num1; num2=num1; do { rnum=rnum*10; int digit=num1%10; rnum+=digit; num1/=10; } while(num1); cout << "The integer you typed is " << num2 << "." << endl; cout << "The reversed integer is " << rnum << "." << endl; getch(); }

30. Program to enter an integer and print its total value based on the formula 'x - 1/3!x^3 + 1/5!x^5 - 1/7!x^7 + 1/9!x^9'
#include <iostream.h> #include <conio.h> #include <math.h> int main() {

clrscr(); float factorial=1; float num,tot,term,total; int i,n=20,index,j=1; cout << "Enter a single-digit integer : \n"; cin>>num; tot=num; total=num; for(i=2,index=3;i<=n;i++,index+=2) { for(j=1,factorial=1;j<=index;j++) factorial*=j; tot=tot*pow((double)(-1),(double)(2*i-1))*num*num; term=tot/factorial; total+=term; } cout << "Total = " << total << endl; getch(); return 0; }

31 Implement the concept of inheritance

#include <iostream> using namespace std; class base { int i, j; public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; } }; // inheritance class derived : public base { int k; public: derived(int x) { k = x; } void showk() { cout << k << "\n"; } }; int main() { derived ob(3); ob.set(1, 2); // access member of base ob.show(); // access member of base ob.showk(); return 0; } // uses member of derived class

32. Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also.
#include <iostream.h> #include <conio.h>

int main() { clrscr(); int A[10][10],m,n,x,y,sum=0; //Create a Matrix A cout << "Enter number of rows and columns in Matrix A : \n"; cin>>n>>m; cout << "Enter elements of Matrix A : \n"; for(x=1;x<n+1;++x) for(y=1;y<m+1;++y) cin>>A[x][y]; //Find sum of each row for(x=1;x<n+1;++x) { A[x][m+1]=0; for(y=1;y<m+1;++y) A[x][m+1]=A[x][m+1]+A[x][y]; } //Find sum of each column for(y=1;y<m+1;++y) { A[n+1][y]=0; for(x=1;x<n+1;++x) A[n+1][y]+=A[x][y]; } cout << "\nMatrix A, Row Sum (Last Column)" << " and Column Sum (Last Row) : \n"; for(x=1;x<n+1;++x) { for(y=1;y<m+2;++y) cout << A[x][y] << " "; cout << "\n"; } //Print sum of each column x=n+1; for(y=1;y<m+1;++y) cout << A[x][y] << " "; cout << "\n"; if(m==n) { for(x=1;x<m+1;x++) for(y=1;y<n+1;y++) if(x==y) sum+=A[x][y]; else if(y==m-(x+1)) sum+=A[x][y]; } cout << "Sum of diagonal elements is : " << sum << endl; getch(); return 0; }

33. Program to convert days into years and weeks

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

void main() { clrscr(); int days,years,weeks,num1; cout << "Enter the number of days : " << endl; cin>>days; years=days/365; num1=days-(years*365); weeks=days/7; num1=days-(weeks*7); cout << days << " days = " << endl; cout << weeks << " weeks OR " << endl; cout << years << " years." << endl; getch();

} 34. Program to find the total days in the year till date
#include <iostream.h> #include <conio.h> void main() { clrscr(); int day,month,total; int days_per_month[12]={31,28,31,30,31,30,31,31,30,31,30,31}; cout << "Enter the month : " << endl; cin>>month; cout << "Enter the day : " << endl; cin>>day; total=day; for(int x=0;x<month-1;x++) total+=days_per_month[x]; cout << "The number of days in this year till date = " << total << endl; getch(); }

35. Program to print the first 10 lines of pascal's triangle

#include <iostream.h> #include <conio.h> #include <iomanip.h> long triangle(int x,int y); int main() { clrscr(); const lines=10; for (int i=0;i<lines;i++) for (int j=1;j<lines-i;j++) cout << setw(2) << " "; for (int j=0;j<=i;j++) cout << setw(4) << triangle(i,j); cout << endl; getch(); } long triangle(int x,int y) { if(x<0||y<0||y>x) return 0;