book on c++ programs

407
// program go print the Transpose of 2*3 matrix #include<stdio.h> #include<conio.h> void main () { clrscr(); int a[2][3]; int i,j; printf("\n enter the value of matrix "); for(i=0;i<2;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("\n The matrix is \n"); for(i=0;i<2;i++) { for (j=0;j<3;j++) { printf("\t%d",a[i][j]); printf(" "); } printf("\n"); } printf("\n The transpose of matrix is \n "); for(j=0;j<3;j++) { for(i=0;i<2;i++) { printf("\t%d",a[i][j]); printf(" "); } printf("\n"); } getch(); }

Upload: testest1

Post on 22-Dec-2015

9 views

Category:

Documents


2 download

DESCRIPTION

Tonnes of Sample C++ Programs for kids to learn and copy. Use this for education and learning. C Plus Plus Programming Language.

TRANSCRIPT

Page 1: BOOK on C++ Programs

// program go print the Transpose of 2*3 matrix

#include<stdio.h>#include<conio.h>void main (){clrscr();int a[2][3];int i,j;printf("\n enter the value of matrix ");for(i=0;i<2;i++){for(j=0;j<3;j++){scanf("%d",&a[i][j]);}}printf("\n The matrix is \n");for(i=0;i<2;i++){for (j=0;j<3;j++){printf("\t%d",a[i][j]);printf(" ");}printf("\n");}printf("\n The transpose of matrix is \n ");for(j=0;j<3;j++){for(i=0;i<2;i++){printf("\t%d",a[i][j]);printf(" ");}printf("\n");}getch();}

Page 2: BOOK on C++ Programs

// PROGRAM TO CHECK PRIME NO

#include<stdio.h>#include<conio.h>void main(){clrscr();int x=2,n;printf("enter the no");scanf("%d",&n);if(x<=n-1&&n%x!=0)printf("no is prime");elseprintf("no is not prime ");x=x+1;getche();}

Page 3: BOOK on C++ Programs

// PROGRAM TO PRINT FIBONACCI SERIES

#include<iostream.h>#include<conio.h>void main(){clrscr();int a=0,b=1,c=0,p,n;cout<<"Enter the value of n: ";cin>>n;cout<<a<<" "<<b<" ";while(c<=n){c=a+b;a=b;b=c;cout<<" "<<c;}getch();}

Page 4: BOOK on C++ Programs

// Program to print the sum of two numbers.

#include<iostream.h>void main(){int a,b,c;cout<<"enter the value of a ";cin>>a;cout<<"Enter the value of b ";cin>>b;c=a+b;cout<<"c=";cout<<c;}

Page 5: BOOK on C++ Programs

// Program to print the Square of any number.

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

void main(){clrscr();int n;cout<<"Enter the value of n ";cin>>n;cout<<"Square is ";cout<<n*n;}

Page 6: BOOK on C++ Programs

// Program to print the reverse of a no. taken as input from Keyboard.

#include <iostream.h>#include<conio.h>void main(){clrscr();int a,b,c,d=0,n;cout<<"Enter the value of n \n";cin>>n;a=n;do{n=a;a=n/10;b=a*10;c=n-b;d=(d+c)*10;}while(a!=0);d=d/10;cout<<"\n Reverse is "<<d<<endl;getch();}

Page 7: BOOK on C++ Programs

// Program to print Average of all the even numbers between 1-20

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,s=0,a=0;for (i=1;i<=20;i++){if (i%2==0){s=s+i;a++;}}cout<<" SUM=" <<s;cout<<"\n Average = "<<s/a;getch();}

Page 8: BOOK on C++ Programs

// Program to print weather the number entered is Anstrom or not.

#include<iostream.h>#include<conio.h>void main(){clrscr();int a,aa,b,n,s=0;cout<<"Enter the value of n: ";cin>>n;b=n;aa=n;while (b!=0){a=n%10;s=s+(a*a*a);b=n/10;n=b;}if (aa==s){cout<<"The Number is Anstrom.\n";}else cout<<" The Number is not a Anstrom\n";getch();}

Page 9: BOOK on C++ Programs

// Program to convert Hours to Seconds Using Functions

#include<iostream.h>#include<conio.h>int sec(){clrscr();int m,s,t;cout<<"Enter the Number of Hours: ";cin>>m;s=60;t=m*s;return t;}

void main(){cout<<sec();getch();}

Page 10: BOOK on C++ Programs

// Program to calculate the Sin value of any number.

#include<iostream.h>#include<math.h>#include<conio.h>void main(){clrscr();double n;double p;cout<<"enter the value of num"<<endl;cin>>n;p=sin(n);cout<<p;getch();}

Page 11: BOOK on C++ Programs

// Program to Sort 5 elements using Bubble Sort

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){clrscr();int a[5],i,j,t;for(i=0; i<5;i++){cout<<"Enter the value: ";cin>>a[i];}for(i=0;i<5;i++){for(j=0;j<4;j++){if(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}}for(i=0;i<5;i++)cout<<a[i];getch();}

Page 12: BOOK on C++ Programs

//Program to print the sum of even & odd terms using GOTO

#include<iostream.h>#include<conio.h>void main(){int n,c=0,se=0,so=0,m;clrscr();cout<<"\nEnter no. of terms:";cin>>n;k:c=c+1;cout<<"\nEnter numbers:";cin>>m;if(m%2==0)se=se+m;elseso=so+m;if(c<n) goto k;cout<<"\nSum of even num is:"<<se;cout<<"\nsum of odd num is:"<<so;getch();}

Page 13: BOOK on C++ Programs

//Program to print the Fibonacci Series till n terms

#include<iostream.h>#include<conio.h>void main(){clrscr();int a,b,c,n,d=2;cout<<"\nEnter no. of terms:";cin>>n;a=0;b=1;cout<<a<<b;k:c=a+b;d=d+1;a=b;b=c;cout<<c;if(d<n) goto k;getch();}

Page 14: BOOK on C++ Programs

//Program to print Albhabets of English from a-z

#include<iostream.h>#include<conio.h>void main(){clrscr();char i;for(i='a';i<='z';i++)cout<<i;}

Page 15: BOOK on C++ Programs

// Program to Print the design on screen using arc

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

int main(void){ /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int xmax, ymax;

/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");

/* read result of initialization */ errorcode = graphresult(); /* an error occurred */ if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); }

setcolor(getmaxcolor()); xmax = getmaxx(); ymax = getmaxy();

/* draw a diagonal line */ for(int y=100;y<400;y+=3) { delay(10); line(100, y, 300, 255); } for(y=100;y<400;y+=3) { delay(10); line(y,399,300,255); } for(y=399;y>100;y-=3) { delay(10); line(400,y,300,255); } for(y=400;y>100;y-=3) { delay(10); line(y,100,300,255); } /* clean up */ getch(); closegraph(); return 0;

Page 16: BOOK on C++ Programs

}

Page 17: BOOK on C++ Programs

//Program to calculate the factorial of first 10 terms

#include <iostream.h>#include<conio.h>void main (){clrscr();int a=0,b=1;long int f=1,s=1;while (a<10){a=a+1;b=0;s=1;while (b<a){b=b+1;s=s*b;}f=f+s;}cout<<"The factorial of first 10 terms is :\n";cout<<f<<endl;getch();}

Page 18: BOOK on C++ Programs

//Program to show different types of Cursors in C++

#include <conio.h>

int main(void){ clrscr(); /* Display the normal cursor */ cprintf("\n\rNormal Cursor: "); getch();

/* Turn off the cursor */ _setcursortype(_NOCURSOR); cprintf("\n\rNo Cursor : "); getch();

/* Switch to a solid cursor */ _setcursortype(_SOLIDCURSOR); cprintf("\n\rSolid Cursor : "); getch();

/* Switch back to the normal cursor */ _setcursortype(_NORMALCURSOR); cprintf("\n\rNormal Cursor: "); getch();

return 0;}

Page 19: BOOK on C++ Programs

// Program to print the average of any 5 numbers.#include <iostream.h>#include<conio.h>void main (){clrscr();int a[5],i,s=0;float avg;for (i=0;i<5;i++){cout<<"Enter "<<i+1<<" Number\n";cin>>a[i];}for (i=0;i<5;i++){s=s+a[i];}avg =s/5;cout<<"The average of the nos. U have entered is: "<<avg<<"\n";getch();}

Page 20: BOOK on C++ Programs

//Program to arrange the numbers in Ascending order

#include<iostream.h>#include<conio.h>void main (){clrscr();int a[7],i,b,c;for (i=0;i<7;i++){cout <<"Enter the no. \n";cin>>a[i];}for (i=0;i<6;i++){for (b=i+1;b<7;b++){if (a[i]>a[b]){c=a[i];a[i]=a[b];a[b]=c;}}}cout <<"Here R the nos. in ascending order:\n";for (i=0;i<7;i++){cout<<a[i]<<"\n";}getch();}

Page 21: BOOK on C++ Programs

//Program to print all ASCII characters on Screen

#include<iostream.h>#include<conio.h>void main(){clrscr();char h;for (int i=0;i<255;i++){printf("%d %c\t",i,i);}getch();}

Page 22: BOOK on C++ Programs

//Program to calculate the Age in Days

#include<dos.h> #include<iostream.h> #include<conio.h> #include<math.h> #include<process.h> #include<stdio.h> void main() { int d,m,y,td,ty,days,rem_days,rem_months,rem_years,i,days_month,

days_tdays; double tt,tm; clrscr(); cout<<"Enter your Day of Birth "; cin>>d; cout<<"\n Enter your Month Of Birth "; cin>>m; cout<<"\n Enter your year of Birth 4 digs."; cin>>y; cout<<" \nEnter today's Date"; cin>>td; cout<<"\n Enter today's Month "; cin>>tm; cout<<"\n Enter today's Year "; cin>>ty; if (m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) days=31; else if (m==2) days=28; else days=30; if (tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm== 10 ||

tm==12 ) rem_days=31; else if (tm==2) rem_days=28; else rem_days=30; rem_years=ty-(y+1); rem_months = 12- (m+1);

int sum=0; for (i=(m+1) ; i<=tm ; i++) { if((i%4)==0) sum++; } days_month=0; for(i=(m+1);i<=12;i++) { if (i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12) days_month=days_month+31; else if (i==2) days_month+=28; else days_month+=30; } days_tdays=0; for(i=1;i<tm;i++) {

Page 23: BOOK on C++ Programs

if (i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12) days_tdays+=31; else if (i==2) days_tdays+=28; else days_tdays+=30; } tm=rem_years*365; tt=tm+(days_month+days_tdays+td+days-d); clrscr(); for(i=0;i<=tt;i++) { delay (1); gotoxy (40,12); cout<<i; } for (i=0;i<10;i++) { delay (100); gotoxy(41,13) ; cout<<" Days! is your Age..."; delay (100); gotoxy(41,13); cout<<" "; } getch(); }

Page 24: BOOK on C++ Programs

//Program to print the text in color and color background

#include <conio.h>#include<iostream.h>int main(void){ int i, j;

clrscr(); for (i=0; i<18; i++) { cout<<i<<" ";

for (j=0; j<70; j++) cprintf("C");

cprintf("\r\n"); textcolor(i+1); textbackground(i); } textcolor(15); textbackground(0); getch(); return 0; }

Page 25: BOOK on C++ Programs

//Program to make BAR driven menu.

#include<iostream.h>#include<conio.h>#include<process.h>void main(){clrscr();int i=1,j=3;char ch;gotoxy(i,j);textbackground(RED);cprintf(" ");for(;;){gotoxy(16,24);cout<<"Use 8 for up and 2 for down from Numeric Keypad.";got:gotoxy(1,1);cout<<"\t\t\t\tMENU\n\n";cout<<"1. Something 1\n";cout<<"2. Something 2\n";cout<<"3. Something 3\n";cout<<"4. Something 4\n";cout<<"5. Exit\n";ch=getch();switch(ch){case '2':if(j>=7)goto got;gotoxy(i,j++);textbackground(BLACK);cprintf(" ");gotoxy(i,j);textbackground(RED);cprintf(" ");break;case '8':if(j<=3)goto got;gotoxy(i,j--);textbackground(BLACK);cprintf(" ");gotoxy(i,j);textbackground(RED);cprintf(" ");break;case '3':exit(0);break;case 13:gotoxy(10,10);if(j==3)// option 1cout<<"1";if(j==4)// option 2cout<<"2";

Page 26: BOOK on C++ Programs

if(j==5)// option 3cout<<"3";if(j==6)// option 4cout<<"4";if(j==7)// option 5{delay(300);exit(0);}break;}}textcolor(15);textbackground(0);}

Page 27: BOOK on C++ Programs

//Program to make Customized BAR driven menu

#include<iostream.h>#include<conio.h>#include<process.h>void case1(){}void case2(){}void case3(){}void case4(){}void case5(){}void case6(){}void case7(){}void main(){clrscr();int yu;cout<<"Enter ";cin>>yu;int i=25,j=7,num=15;gotoxy(i,j);textbackground(num);cprintf(" ");for(;;){got:gotoxy(15,22);cout<<"Use UP & DOWN Arrow key for scrolling! Press 8 for Exit";char ch=getch();switch(ch){case '1':case1();getch();break;case '2':case2();getch();break;case '3':case3();getch();break;case '4':case4();getch();break;case '5':case5();getch();break;case '6':case6();getch();break;case '7':

Page 28: BOOK on C++ Programs

case7();getch();break;case 'P':if(j>=yu+6){gotoxy(47,5);textcolor(BLUE);cprintf("%d",1);textcolor(15);i=25;gotoxy(i,j++);textbackground(BLACK);cprintf(" ");j=7;gotoxy(i,j);textbackground(num);cprintf(" ");goto got;

}gotoxy(47,5);textcolor(BLUE);cprintf("%d",j-5);textcolor(15);gotoxy(i,j++);textbackground(BLACK);cprintf(" ");gotoxy(i,j);textbackground(num);cprintf(" ");break;case '8':exit(0);break;case 'H':gotoxy(47,5);textcolor(BLUE);cprintf("%d",j-7);textcolor(15);if(j<=7){gotoxy(47,5);textcolor(BLUE);cprintf("%d",yu);textcolor(15);gotoxy(i,j--);textbackground(BLACK);cprintf(" ");j=yu+6;gotoxy(i,j);textbackground(num);cprintf(" ");goto got;}gotoxy(i,j--);textbackground(BLACK);

Page 29: BOOK on C++ Programs

cprintf(" ");gotoxy(i,j);textbackground(num);cprintf(" ");break;case 13:textcolor(15);textbackground(0);gotoxy(10,10);if(j==7)// option 1case1();if(j==8)// option 2case2();if(j==9)// option 3case3();if(j==10)// option 4case4();if(j==11)// option 5case5();if(j==12)case6();if(j==13){exit(0);}break;}}textbackground(0);}

Page 30: BOOK on C++ Programs

// program to print the given design

*************************

#include<iostream.h>#include<conio.h>void main(){int i,j,k,l;clrscr();for(i=1;i<=5;i++){for(j=1;j<=i;j++)cout<<"*";cout<<"\n";}for(k=4;k>=1;k--){for(l=1;l<=k;l++)cout<<"*";

cout<<"\n";}}

Page 31: BOOK on C++ Programs

//Program to calculate the days of Birth without current date entry

#include<dos.h> #include<iostream.h> #include<conio.h> #include<math.h> #include<process.h> #include<stdio.h> void main() { int d,m,y,td,ty,days,rem_days,rem_months,rem_years,i,days_month,

days_tdays; double tt,tm; clrscr(); cout<<"Enter your Day of Birth "; cin>>d; cout<<"\n Enter your Month Of Birth "; cin>>m; cout<<"\n Enter your year of Birth 4 digs."; cin>>y; struct dosdate_t d4; _dos_getdate(&d4); ty=d4.year; td=d4.day; tm=d4.month;

/*cout<<" \nEnter today's Date"; cin>>td; cout<<"\n Enter today's Month "; cin>>tm; cout<<"\n Enter today's Year "; cin>>ty;*/ if (m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) days=31; else if (m==2) days=28; else days=30; if (tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm== 10 ||

tm==12 ) rem_days=31; else if (tm==2) rem_days=28; else rem_days=30; rem_years=ty-(y+1); rem_months = 12- (m+1);

int sum=0; for (i=(m+1) ; i<=tm ; i++) { if((i%4)==0) sum++; } days_month=0; for(i=(m+1);i<=12;i++) { if (i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12) days_month=days_month+31; else if (i==2) days_month+=28;

Page 32: BOOK on C++ Programs

else days_month+=30; } days_tdays=0; for(i=1;i<tm;i++) { if (i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12) days_tdays+=31; else if (i==2) days_tdays+=28; else days_tdays+=30; } tm=rem_years*365; tt=tm+(days_month+days_tdays+td+days-d); clrscr(); for(i=0;i<=tt;i++) { delay (1); gotoxy (40,12); cout<<i; } for (i=0;i<10;i++) { delay (100);

gotoxy(41,13) ; cout<<" Days! is your Age..."; delay (100); gotoxy(41,13); cout<<" "; } getch(); }

Page 33: BOOK on C++ Programs

//Program to sort the numbers using BUBBLE SORTING

#include<stdio.h>#include<conio.h>main(){clrscr();int i,j,n,a[100],temp;printf("\nEnter the no. of elements in the array.");scanf("%d",&n);for(i=0;i<n;i++){printf("\nElement %d: ",i+1);scanf("%d",&a[i]);}for(i=0;i<n;i++){for(j=0;j<n-1;j++){if(a[j]>a[j+1]){temp=a[j];a[j]=a[j+1];a[j+1]=temp;}}}printf("Sorted array is:\n");for(i=0;i<n;i++){printf("%d\n",a[i]);}getch();}

Page 34: BOOK on C++ Programs

//Program to Convert the Char number to integer number

#include<iostream.h>#include<stdio.h>#include<conio.h>#include<string.h>void main(){clrscr();char year[5];int yr,l1;int l=0;gets(year);l1=strlen(year);int i=0;

while(i<l1){switch(year[i]){case '0':l=(l*10)+0;break;case '1':l=(l*10)+1;break;

case '2':l=(l*10)+2;break;

case '3':l=(l*10)+3;break;

case '4':l=(l*10)+4;break;

case '5':l=(l*10)+5;break;

case '6':l=(l*10)+6;break;

case '7':l=(l*10)+7;break;

case '8':l=(l*10)+8;break;

case '9':l=(l*10)+9;

Page 35: BOOK on C++ Programs

break;

}i++;}cout<<l;getch();}

Page 36: BOOK on C++ Programs

//Program to Show a 15 second clock

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

int main(void){ /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int midx, midy; int radius = 100;

/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");

/* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ }

midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor());

/* draw the circle */circle(290,250,100);

int b=150;for(int u=290;u<=390;u+=5){delay(1000);setcolor(15);line(290,250,u,b);setcolor(0);line(290,250,u-5,b-5); b+=5; } /* clean up */ getch(); closegraph(); return 0;}

Page 37: BOOK on C++ Programs

//Program to display the text in all possible colors

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

void main(){clrscr();for(int i=0;i<20;i++){textcolor(i);cout<<i<<" ";cprintf("Arvind\n");}getch();textcolor(15);}

Page 38: BOOK on C++ Programs

//Program to show the use of Constructor & Destructor

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

struct time{private:

inthh,mm,ss;

public:time(){

hh = mm = ss = 0;cout<<"\n CONSTRUCTOR CALLED ....";

}~time(){

cout<<"\n DESTRUCTOR CALLED ....";}void display();

};void time::display(){

cout<<"\n DISPLAY DEFAULT TIME :::";cout<<hh<<":"<<mm<<":"<<ss;

}void main(){

clrscr();time

t;t.display();

}

Page 39: BOOK on C++ Programs

//Program to check for the Prime number using Classes

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

class num{private:

intn,prime;

public:void getdata();void result();

};

void num::getdata(){

cout<<"\n ENTER THE NUMBER TO BE CHECKED ";cin>>n;

for( int i = 2; i < sqrt(n); i++ ){

if( n % i == 0 ){

prime = 0;}

}}

void num::result(){

if(prime)cout<<n<<"\nIS A PRIME NUMBER ";

elsecout<<n<<"\n IS NOT A PRIME NUMBER";

}

void main(){

clrscr();getch();num

n;n.getdata();n.result();

}

Page 40: BOOK on C++ Programs

//Program to find the Cube of any number using functions

#include<iostream.h>#include<conio.h>void main(){

clrscr();float cube( float);float

y,a;

cout<<"\n ENTER THE NUMBER WHOSE CUBE IS TO BE FOUND :";cin>>a;y = cube(a);

cout<<"\n THE CUBE OF THE NUMBER IS:"<<y;}

float cube( float a ){

float n;n = a*a*a ;return (n);

}

Page 41: BOOK on C++ Programs

//Program to print the table of n number

#include <iostream.h>#inc;ude<conio.h>void main (){clrscr();int n,a=0,b;cout <<"Enter the value of n "<<endl;cin>>n;cout <<"HERE IS THE TABLE OF "<<n<<"\n";while (a<10){a=a+1;b=n*a;cout<<n<<"*"<<a<<"="<<b<<"\n";}getch();}

Page 42: BOOK on C++ Programs

//Program to Enter & Display in the form of Astrics (*)

#include<iostream.h>#include<conio.h>void main(){clrscr();char c,char2[20];int i,j;i=0;do{c=getch();cout<<"*";char2[i++]=c;}while((c>=65&&c<=90)||(c>=97&&c<=122)&&(i<10));clrscr();for(j=0;j<i-1;j++)cout<<char2[j];getch();}

Page 43: BOOK on C++ Programs

//Program to make design in C++ using Graphics

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

int main(void){ int gdriver = DETECT, gmode, errorcode; int xmax, ymax; initgraph(&gdriver, &gmode, ""); errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } setcolor(getmaxcolor()); xmax = getmaxx(); int i,j; ymax = getmaxy();delay(1000); for(i=0;i<=100;i+=2) { for(j=0;j<=650;j++) line(0,i,j,i); } for(i=480;i>=380;i-=2) { for(j=0;j<=650;j++) line(100,i,j,i); }

for(i=0;i<=100;i+=2) { for(j=199;j<=380;j++) line(i,100,i,j); } for(i=650;i>=550;i-=2) { for(j=199;j<=380;j++) line(i,100,i,j); }

getch(); closegraph(); return 0;}

Page 44: BOOK on C++ Programs

//Program to Display the use of Password in C++

#include<graphics.h>#include<fstream.h>#include<iostream.h>#include<stdio.h>#include<conio.h>#include<dos.h>#include<process.h>#include<stdlib.h>#include<string.h>class member{private:

public:int k,k1; char ps2[10],ps[10],ans[30],ch; char js[10],ans1[30];

void welcome() { strcpy(js,NULL); clrscr(); int i,j; textcolor(5); gotoxy(25,11); cprintf("Enter password");

char c,char2[20];i=0;do{textcolor(15);c=getch();if(c==13){textcolor(15);break;}cout<<"*";js[i]=c;i++;}while((c>=65&&c<=90)||(c>=97&&c<=122)&&(i<10));textcolor(15);} //end of welcome()

void alarm() { fstream fin;

fin.open("pwd.txt",ios::in); fin>>ps>>ans>>k; fin.close();

sound(2000); zz: clrscr(); textcolor(15+128);

Page 45: BOOK on C++ Programs

cprintf(" Insert Keyword to Stop Alarm"); cin>>k1; if(k1==k) {

textcolor(15); nosound();} else { cout<<" Keyword Mismatch"; getch(); goto zz; } }//end of alarm

void hide(){clrscr();welcome();strcpy(ps,js);clrscr();textcolor(15);gotoxy(10,15);cprintf(" Question: What is your Pet's name");gets(ans);gotoxy(10,18);cprintf("Enter Keyword");cin>>k;}//end of hide

void chpw(){ clrscr(); welcome(); fstream fin; fin.open("pwd.txt",ios::in); fin>>ps>>ans>>k; fin.close(); fstream fout; fout.open("pwd.txt",ios::out); fout<<js<<'\t'<<ans<<'\t'<<k; fout.close(); cout<<"\n\n\nPassword Changed"; getch(); }//end of chpw

void chan(){ clrscr(); fstream fin; fin.open("pwd.txt",ios::in); fin>>ps>>ans>>k;

Page 46: BOOK on C++ Programs

fin.close(); fstream fout; cprintf(" Question: What is your Pet's name"); gets(ans1); fout.open("pwd.txt",ios::out); fout<<ps<<'\t'<<ans1<<'\t'<<k; fout.close(); cout<<"\n\n\n\nAnswer Changed"; getch(); }//end of chan

void chky(){ clrscr(); fstream fin; fin.open("pwd.txt",ios::in); fin>>ps>>ans>>k; fin.close(); fstream fout; gotoxy(10,15); cout<<" Enter the Keyword"; cin>>k1; fout.open("pwd.txt",ios::out); fout<<ps<<'\t'<<ans<<'\t'<<k1; fout.close(); cout<<"\n\n\nKeyword changed"; getch(); }//end of chky

void vwall(){ clrscr(); fstream fin; fin.open("pwd.txt",ios::in); fin>>ps>>ans>>k; fin.close(); textcolor(15); gotoxy(10,10); cout<<" Password - "; cout<<ps; gotoxy(10,12); cout<<" Answer - "; puts(ans); // cout<<"\t"<<ans; gotoxy(10,15); cout<<" Keyword - "; cout<<k; getch(); clrscr(); }//end of vwall

Page 47: BOOK on C++ Programs

}x;//end of class

void main() { int a=0,b; char ch; textbackground(1); fstream fout; fstream fin; fin.open("pwd.txt",ios::in); if(fin==NULL) { fin.close(); x.welcome(); x.hide(); fout.open("pwd.txt",ios::out); fout<<x.ps<<'\t'<<x.ans<<'\t'<<x.k; fout.close();

main(); }//if ends here qq: x.welcome(); fflush(stdin); fin>>x.ps>>x.ans>>x.k; fin.close(); a++; if(a>=3) { x.alarm(); }textbackground(1); if(strcmp(x.ps,x.js)!=0 && a<3) {

gotoxy(10,20); textcolor(15+128); cprintf("Password Incorrect..."); gotoxy(10,22); textcolor(15); cprintf("Do you want to answer the question(y-yes)"); ch=getche(); clrscr(); if(ch=='y' || ch=='Y') { textcolor(15); gotoxy(10,10); cprintf("Question: What Is Your Pet's Name"); cout<<"\n"; cprintf("Answer: "); gets(x.ans1); if(strcmp(x.ans,x.ans1)!=0) x.alarm(); else goto s; }//end of if else goto qq; }

Page 48: BOOK on C++ Programs

else{s:

clrscr(); gotoxy(35,10); cout<<"CORRECT ENTRY"; gotoxy(38,12); textcolor(15+BLINK); cprintf("Welcome..."); textcolor(15); gotoxy(10,20); cout<<"Do you want to Enter in Setup (Press Enter) (n) to

exit "; ch=getche(); if(ch=='n' || ch=='N') {

fin.close(); textcolor(15+BLINK); gotoxy(50,22); cprintf(" A Programe By:"); gotoxy(60,23); cprintf("Diveymaan"); getch(); exit(0); }

do { clrscr(); textbackground(1); textcolor(15); cout<<"\n 1- To Change Password"

<<"\n 2- To Change Answer"<<"\n 3- To Change Keyword"<<"\n 4- To view Password ,answer, keyword"<<"\n 5- Exit";cout<<"\n";

cin>>b; switch(b){ case 1:

x.chpw();break;

case 2:x.chan();break;

case 3:x.chky();break;

case 4:x.vwall();break;

case 5:fout.close();fin.close();

Page 49: BOOK on C++ Programs

textcolor(15+128);gotoxy(50,20);cprintf(" A Programe By");gotoxy(55,21);cprintf(" Arvind Gaba");getch();exit(0);break;

default:gotoxy(15,15);cout<<"You Have Entered Wrong Option";break;}} while(b!=5);getch();

} }

Page 50: BOOK on C++ Programs

//Program to calculate the Difference of 2 Matrix of 3x3

#include<conio.h>#include <iostream.h>void main(){clrscr();int a[3][3],i,j,b[3][3],c[3][3];for (i=0;i<3;i++){for (j=0;j<3;j++){cout<<"Enter the values of a";cin>>a[i][j];}

}for (i=0;i<3;i++){for (j=0;j<3;j++){cout<<"Enter the values of b";cin>>b[i][j];}}for (i=0;i<3;i++){for (j=0;j<3;j++){c[i][j]=a[i][j]-b[i][j];}}for (i=0;i<3;i++){for (j=0;j<3;j++){cout<<c[i][j]<<" ";}cout<<"\n";}getch();}

Page 51: BOOK on C++ Programs

//Program to Make the Welcome screen of C++ Project

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

int main(void){char by[]={'B','Y','-'};char namee[]={'M','A','M','T','A',' ','(','P','G','D','C','A',')'}; /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int xmax, ymax;

/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");

/* read result of initialization */ errorcode = graphresult(); /* an error occurred */ if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); }

setcolor(getmaxcolor()); xmax = getmaxx(); ymax = getmaxy(); /* draw a diagonal line */for(int i=10;i<800;i+=10){delay(50); line(0,i,ymax-i,xmax-i);}delay(1000);for(i=10;i<800;i+=10){delay(50); line(i,i,xmax,ymax-i); } textcolor(15);

delay(600); gotoxy(50,3);printf("WELCOME TO THE PROJECT"); delay(600); gotoxy(58,4);

cout<<"OF"; delay(600); gotoxy(50,5); cout<<"FEE ACCOUNTING SYSTEM";

Page 52: BOOK on C++ Programs

delay(800); gotoxy(5,23); for(i=0;i<3;i++){ cout<<by[i]; delay(400); } gotoxy(5,24); for(i=0;i<13;i++) { cout<<namee[i]; delay(400); }/* clean up */ getch(); for(i=0;i<80;i++) { for(int yt=0;yt<35;yt++) { delay(1); gotoxy(i,yt); cout<<" "; } } closegraph(); return 0;}

Page 53: BOOK on C++ Programs

//Program to Print the Following Design

***** *** * *** *****

#include<iostream.h>#include<conio.h>void main(){clrscr();int a=1,i=0,j=0,k=5;do{

a=0,j=0;i++;do{j++;cout<<" ";}while(j<i);

do {cout<<"*";a++;}while(a<k);k=k-2;cout<<"\n";

}while(i<3);i=3,k=3,j=0;do{

j=0,i--;a=0;do{j++;cout<<" ";

}while(j<i);do{a=a+1;cout<<"*";}while(a<k);k=k+2;cout<<"\n";}while(i>1);getch();}

Page 54: BOOK on C++ Programs

//Program to make C++ as a Text Editor

#include<iostream.h>#include<stdio.h>#include<conio.h>#include<process.h>void main(){clrscr();char text[1000];strcpy(text,NULL);

char ch;int i=0,j=1,ii[1000];ii[0]=50;for(i=0;i<1000;i++){text[i]=NULL;ii[i]=0;}i=0;int counter=0;

for(;;){top:ch=getch();if(ch=='M'){gotoxy(i,j);cout<<text[i-1];goto top;i++;}//i++;if(ch=='K'){if((i<=1) && (j==1))goto top;if(i>1){gotoxy(i,j);cout<<text[i];i--;goto top;}elsei=--ii[--j];goto top;}i++;gotoxy(25,15);cout<<i;gotoxy(i,j);cout<<ch;

Page 55: BOOK on C++ Programs

if(ch==13){text[counter]='*';counter++;ii[j]=i;cout<<"\n";j++;i=0;goto top;}if(ch==8){text[--counter]=NULL;gotoxy(--i,j);cout<<" ";if(i==0){if(j==1)goto top;i=ii[--j]-1;goto top;}if(i>0)i--;goto top;}if(ch=='$'){//puts(text);//getch();exit(0);}text[counter]=ch;counter++;}

}

Page 56: BOOK on C++ Programs

//Program to display the Circle with Effects

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

int main(void){ /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int midx, midy; int stangle = 45, endangle = 135; int radius = 100;

/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

/* read result of initialization */ errorcode = graphresult(); /* an error occurred */ if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch();

exit(1); /* terminate with an error code */ }

midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor());

/* draw arc */ getch(); textcolor(5); for(int ii=220;ii>=0;ii--) { arc(300,200,ii,360,150);

delay(20);}for(ii=220;ii>=0;ii--){for(int jj=145;jj>=5;jj-=5)arc(300,200,ii,360,jj);delay(5);}

/* clean up */ getch(); closegraph(); return 0;}#include<iostream.h>#include<conio.h>

Page 57: BOOK on C++ Programs

#include<stdio.h>#include<process.h>#include<string.h>#include<dos.h>void main(){clrscr();for(int i=0;i<10;i++){delay(10);gotoxy(30,12);cout<<"Arvind Gaba";delay(500);gotoxy(30,12);cout<<" ";delay(10);gotoxy(30,13);cout<<"Arvind Gaba";delay(500);gotoxy(30,13);cout<<" ";delay(10);gotoxy(30,14);cout<<"Arvind Gaba";delay(500);gotoxy(30,14);cout<<" ";delay(10);gotoxy(30,15);cout<<"Arvind Gaba";delay(500);gotoxy(30,15);cout<<" ";delay(10);gotoxy(30,16);cout<<"Arvind Gaba";delay(500);gotoxy(30,16);cout<<" ";}getch();}

Page 58: BOOK on C++ Programs

//Program to calculate the Factorial of first 10 Natural Numbers

#include <iostream.h>#include<conio.h>void main (){clrscr();long int a=0,s,f=0,b;while (a<10){a=a+1;b=0;s=1;while (b<a){b=b+1;s=s*b;}f=f+s;}cout<<"The factorial of first 10 terms is :\n";cout<<f<<endl;cout<<"COMPILED BY- ARVIND GABA"<<"\n";}#include<stdio.h>#include<conio.h>int i=1,m=1,n;int fact();main(){clrscr();printf("Enter the no. to calculate the factorial.");scanf(" %d",&n);int f;f=fact();printf("\n%d",f);getch();}fact(){if(i>n)return m;m*=i;i++;fact();}

Page 59: BOOK on C++ Programs

//Program to Print the Fabronacci series of n Terms using Recursion.

#include<stdio.h>#include<conio.h>int a=0,b=1,c=0,n,i=2;void fact();main(){clrscr();printf("\nEnter the no. of terms");scanf("%d",&n);printf("%d %d ",a,b);fact();getch();}void fact(){if(i>n-1)return ;else{c=a+b;printf("%d ",c);a=b;b=c;i++;fact();}}

Page 60: BOOK on C++ Programs

//Program to Display the Big font in C++

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

int main(void){ /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int y = 0; int i; char msg[80];

/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");

/* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } i=8;

settextstyle(TRIPLEX_FONT, HORIZ_DIR, i); gotoxy(10,10); sprintf(msg, "Size: %d", i); outtextxy(1, y, msg); y += textheight(msg); getch();

/* clean up */ closegraph(); cout<<"\n\n\nHY"; getch(); return 0;}

Page 61: BOOK on C++ Programs

//Program to Convert from Hexa Decimal to Decmal Number System.

#include<iostream.h>#include<conio.h>#include<string.h>#include <math.h>#include<stdio.h>void main(){int i=0,bb=0,nn=0,sum=0,a=0,l;char n[10];clrscr();cout<<"Enter the no. in base 16 ";gets(n);l=strlen(n);a=l-1;for (i=0;i<l;i++){if (n[i]=='A')nn=10;else if (n[i]=='B')nn=11;else if (n[i]=='C')nn=12;else if (n[i]=='D')nn=13;else if (n[i]=='E')nn=14;else if (n[i]=='F')nn=15;else nn=n[i];bb=nn*pow(16,a);a--;sum=sum+bb;}cout<<"\nSum=";cout<<sum;getch();}

Page 62: BOOK on C++ Programs

//Sorting Using Insertion Sort

#include<iostream.h>#include<stdio.h>#include<conio.h>main(){clrscr();int i,j,b=0,min=0,loc=0,n,a[100],temp=0;printf("\nEnter the no. of elements in the array.");scanf("%d",&n);for(i=1;i<=n;i++){printf("\nElement %d: ",i);scanf("%d",&a[i]);}a[0]=0;for(i=2;i<=n;i++){for(j=0;j<i;j++){if((a[i]>=a[j])&&(a[i]<=a[j+1])){temp=a[i];a[i]=a[j+1];a[j+1]=temp;}}//j loop}//i loopfor(i=1;i<=n;i++)printf("%d ",a[i]);getch();}

Page 63: BOOK on C++ Programs

//Program to Calculate the Left Digonal Sum of a Matrix

#include<iostream.h>#include<conio.h>void main(){clrscr();int a[3][3],i,j,s=0;for (i=0;i<3;i++){for (j=0;j<3;j++){cout<<"Enter the values of a";cin>>a[i][j];}}for (i=0;i<3;i++){for (j=0;j<3;j++){if (i==j){s=s+a[i][j];}//end of if loop}//end of j loop}//end of i loopcout<<"Sum= "<<s;getch();}

Page 64: BOOK on C++ Programs

//Program to Make the Welcome Screen in C++ Projects

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

int main(void){ /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int xmax, ymax;

/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");

/* read result of initialization */ errorcode = graphresult(); /* an error occurred */ if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); }

setcolor(getmaxcolor()); xmax = getmaxx(); ymax = getmaxy();

for(int ii=100;ii<=350;ii+=5) { for(int j=0;j<=800;j++) { line(0,ii,j,ii); delay(0); } }for(int i=1;i<=58;i++){delay(5);gotoxy(i,11);cout<<"WELCOME TO THE PROJECT";gotoxy(i-1,11);cout<<" ";}for(i=59;i>=1;i--){

delay(10);gotoxy(i,11);cout<<"WELCOME TO THE PROJECT";gotoxy(i+20,11);cout<<" ";

Page 65: BOOK on C++ Programs

}for(i=1;i<30;i++){delay(20);gotoxy(i,11);cout<<"WELCOME TO THE PROJECT";gotoxy(i-1,11);cout<<" "; } delay(200);int aa=1; for(i=80;i>0;i--)

{gotoxy(i,13);delay(25);cout<<" ";gotoxy(aa,15);delay(25);aa++;cout<<" ";t:}

for(i=78;i>=39;i--) { delay(25); gotoxy(i,13); cout<<"OF"; gotoxy(i+2,13); cout<<" "; } delay(300); gotoxy(30,15); cout<<"THEATER MANAGEMENT."; delay(300); char ss[]={'B','Y','-'}; char name[]={'N','A','Z','M','A',' ','P','G','D','C','A'}; gotoxy(65,24); for(i=0;i<3;i++) { delay(250); cout<<ss[i]<<" ";

} gotoxy(65,25); for(i=0;i<11;i++) { delay(200); cout<<name[i]; }delay(300);gotoxy(30,25);cout<<"Press Any Key To Continue...";

getch();int sss=50;int g; for(i=0;i<=25;i++) {

Page 66: BOOK on C++ Programs

for(int yj=0;yj<=80;yj++) { gotoxy(yj,i); cout<<" "; delay(3);} }

for(int t=50;t<=75;t+=5) { for(int j=0;j<=500;j++) { line(t,0,t,j);//vertical line(0,sss,g,sss); //horizon

g+=2; delay(1); } sss+=5; g=0; } int uj; for(i=410;i<=425;i+=5) { for(uj=350;uj<=600;uj++) { delay(1); line(uj,i,uj,i); } } for(i=510;i<=535;i+=5) { for(uj=325;uj<=450;uj++) { line(i,uj,i,uj); delay(1); } } getch(); closegraph(); return 0;}

Page 67: BOOK on C++ Programs

//Program to Create Directory through C++

#include <stdio.h>#include <conio.h>#include <process.h>#include <dir.h>

#define DIRNAME "c:\\fom\\testdir.$$$"

int main(void){ int stat;

stat = mkdir(DIRNAME); if (!stat)

printf("Directory created\n"); else { printf("Unable to create directory\n"); exit(1); }

getch(); exit(0); system("dir/p"); getch();

stat = rmdir(DIRNAME); if (!stat)

printf("\nDirectory deleted\n"); else {

perror("\nUnable to delete directory\n"); exit(1); }

return 0;}

Page 68: BOOK on C++ Programs

//Program to Display your Name with Effects

#include<iostream.h>#include<conio.h>#include<dos.h>void main(){clrscr();char a[12]={'A','r','v','i','n','d',' ','G','a','b','a'};for(int i=0;i<11;i++){delay(300);cout<<a[i];}getch();}

Page 69: BOOK on C++ Programs

// Program to find the sum of two Matrix of 3 X 3

#include<stdio.h>#include<conio.h>main(){clrscr();int a[3][3],sum=0,b[3][3],i,j,c[3][3];printf("Enter the Elements\n");for(i=0;i<3;i++){for(j=0;j<3;j++)scanf("%d",&a[i][j]);}printf("\nEnter for 2nd Array\n");for(i=0;i<3;i++){for(j=0;j<3;j++){scanf("%d",&b[i][j]);c[i][j]=a[i][j]+b[i][j];}}printf("\nSum of Matrix is \n");for(i=0;i<3;i++){for(j=0;j<3;j++)printf("%d ",c[i][j]);printf("\n");}getch();}

Page 70: BOOK on C++ Programs

//Program to find the Vowels, Words n Length in a String entered.

#include<string.h>#include<stdio.h>#include<conio.h>main(){clrscr();char str[80];int i,j,l,w=0,s=0;printf("Enter any string ");gets(str);l=strlen(str);for(i=0;i<l;i++){if(str[i]== 'a'||str[i]=='A'|| str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U')w++;if(str[i]==' ')s++;}printf("\nWords=%d",s+1);printf("\nVowels=%d",w);printf("\nLength=%d",l);getch();}

Page 71: BOOK on C++ Programs

//Program to arrange the given elements in both Ascending & Descending order

#include<stdio.h>#include<conio.h>void main(){

int i,j,m,n,a[100];clrscr();printf("Enter the Value of n ");scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);

for(i=0;i<n;i++){for(j=0;j<=i;j++){if(a[i]>a[j]){m=a[i];a[i]=a[j];a[j]=m;}}}printf("\nDescending Order: \n");for(i=0;i<n;i++)printf("%d\t",a[i]);printf("\n");printf("\nAscending Order\n");for(i=n-1;i>=0;i--)

printf("%d\t",a[i]); getch(); }

Page 72: BOOK on C++ Programs

//Program to Enter and Display a Password in C++

#include <conio.h>

int main(void){ char *password;

password = getpass("Input a password:"); cprintf("The password is: %s\r\n", password); return 0;}

Page 73: BOOK on C++ Programs

//Program to Sort 5 elements in Descending Order Bubble Sort

#include <iostream.h>#include<conio.h>void main(){clrscr();int a[5],i,j,t;for (i=0;i<5;i++){cout<<"Enter the value";cin>>a[i];}for (i=0;i<5;i++){for (j=0;j<4;j++){

if (a[j]<a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}//end of j loop}//end of i loopfor (i=0;i<5;i++){cout<<a[i]<<'\n';}getch();}

Page 74: BOOK on C++ Programs

// program to find the root of the quadratic equation

#include<stdio.h>#include<conio.h>#include<math.h>void main(){clrscr();int a,b,c;float d,x1,x2;printf("enter the coefficients of varia.");scanf("%d%d%d",&a,&b,&c);d=sqrt(b*b-4*a*c);if(d>0){x1=-b+d;x2=-b-d;printf("\n the value of discriminante is=%f",d);printf("\n the one root is=%f",x1);printf("\n another root is=%f",x2);} else printf("the roots are imagiary"); }

Page 75: BOOK on C++ Programs

//PROGRAM TO PRINT YOUR NAME AND DETAILS ON THE CENTER OF THE SCREEN//

#include<iostream.h>#include<conio.h>void main(){clrscr();int n,c=35,r=40,s=36,w=38;for(n=1;n<c;n++){cout<<" ";}cout<<"ARVIND GABA";cout<<"\n";for(n=1;n<r;n++){cout<<" ";}cout<<"12 SCI..";cout<<"\n";for(n=1;n<s;n++){cout<<" ";}cout<<"COMPUTER PROJECT";cout<<"\n";for(n=1;n<w;n++){cout<<" ";}cout<<"LETS PROCEED";}

Page 76: BOOK on C++ Programs

// PROGRAM TO PRINT GIVEN DESIGN1111111111111111111111111111111111111111111111111111111

# include <iostream.h>#include<conio.h>void main(){clrscr();long int n,c;n=0,c=0;k:c=c+1;n=(n*10)+1;cout<<n<<"\n";if(c<10) goto k;getche ();}

Page 77: BOOK on C++ Programs

// this prgram will print fibonacci numbers

# include<iostream.h>#include<conio.h>void main(){ clrscr(); int n,a=0,b=1,c,d; cin >>n; cout<<a<<","<<b<<","; d=0; k: d=d+1; c=a+b; cout<<c<<","; a=b; b=c; if (d<n) goto k; }

Page 78: BOOK on C++ Programs

// Program to print the table of odd numbers upto 3.

# include<iostream.h>#include<conio.h>#include<dos.h>void main(){ clrscr(); int i=1,c=-1,s; j: c=c+2; k: s=c*i; textcolor(c); cout<<c<<"*"<<i<<"="<<s; cout<<"\n"; i=i+1; delay(100); if(i<=10) goto k; i=1; if(c<3) goto j;}

Page 79: BOOK on C++ Programs

// Program to print the location of the variable in the computer memory

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j;int a[3][3];int b[3][3];for(i=1;i<=3;i++){for(j=1;j<=3;j++){cout<<"enter the num"<<endl;

cin>>a[i][j];b[j][i]=a[i][j];

}for(i=1;i<=3;i++){for(j=1;j<=3;j++){cout<<b[i]<<endl;}}} getch();}

Page 80: BOOK on C++ Programs

//to print the given design of stars * *** ***** ******* *********

#include<iostream.h>#include<conio.h>#include<dos.h>#include<string.h>#include<stdlib.h>#include<stdio.h>void main(){clrscr();int i,j,k,n=39;int c;for(i=1;i<=9;i=i+2){for(j=1;j<=n;j++){cout<<" ";}for(k=1;k<=i;k++){

cprintf("*");

textcolor(90); textbackground(1);

}cout<<endl;n=n-1;getch();}}

Page 81: BOOK on C++ Programs

// program to print the table of the input num#include<iostream.h>#include<conio.h>void main(){clrscr();int n,p;cout<<"enter the value of n"<<endl;cin >>n;int i=0;do{i++;p=n*i;cout<<n<<"*"<<i<<" "<<"="<<p<<endl;}while (i<=n);getch();

}

Page 82: BOOK on C++ Programs

//WAP TO FIND OUT THE TOTAL NO OF ODD AND EVEN NOS.

#include<iostream.h>#include<conio.h>void main(){int i, n[10] ;int p=0,o=0;for(i=1;i<=10;i++){cout<<"enter the num"<<endl;cin>>n[i];}if(n[i]%2==0){p=p+1;}else{o=o+1;}cout<<"the total; no of the odd num ="<<o<<endl;cout<<"the total no of even num ="<<p<<endl;

getch();}

Page 83: BOOK on C++ Programs

//to print the square of the num if even#include<iostream.h>#include<conio.h>void main(){clrscr();int n[10];int i,s;for(i=0;i<=10;i++){cout<<"enter the num"<<endl;cin>>n[i];if(n[i]%2==0){ s=n[i]*n[i];cout<<s<<endl;}}getch();

}

Page 84: BOOK on C++ Programs

//Program to print the given design************************* #include<iostream.h>#include<conio.h>

void main(){clrscr();int i,j;for(i=1;i<=5;i++){for(j=1;j<=i;j++){cout<<"*";}cout<<endl;}for(i=4;i>=1;i--){for(j=1;j<=i;j++){cout<<"*";}cout<<endl;}getch();}

Page 85: BOOK on C++ Programs

//star design

***************

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j;for(i=1;i<=5;i++){for(j=1;j<=i;j++){cout<<"*";}cout<<"\n";}getch();}

Page 86: BOOK on C++ Programs

//GIVEN DESIGN

IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j;for(i=1;i<=10;i++){for(j=1;j<=i;j++){cout<<"I";}cout<<"\n";}getch();}

Page 87: BOOK on C++ Programs

//PROGRAM TO PRINT THIS SERIES 1 4 9 16 25 16

#include<iostream.h>#include<conio.h>void main(){int i=0,r,c=5,r1;clrscr();k:i=i+1;r=i*i;cout<<r<<" "; if(i<5) goto k; k1:c=c-1; r1=c*c; cout<<r1<<" "; if (c<0) goto k1;}

Page 88: BOOK on C++ Programs

//PROGRAM THAT INPUTS A SERIES OF NOS. FROM THE USER AND DISPLAYS BACK THE SUM OF ALL THE ODDS AND EVEN NUMBERS

#include<iostream.h>#include<conio.h>void main(){int n,c=0,se=0,so=0,m;clrscr();cout<<"\nEnter no. of terms:";cin>>n;k:c=c+1;cout<<"\nEnter numbers:";cin>>m;if(m%2==0)se=se+m;elseso=so+m;if(c<n) goto k;cout<<"\nSum of even num is:"<<se;cout<<"\nsum of odd num is:"<<so;}

Page 89: BOOK on C++ Programs

// A Program to perform all 4 Arthematic Operations of 2 Given Numbers.

#include<conio.h>#include<stdio.h>void main(){clrscr();float a,b,c,d,e;float f1;printf ("Enter two number");scanf("%f%f",&a,&b);c =a + b ;d =b - a ;e =a * b ;

f1 = b/a ;printf("\n sum of two no =%f",c);printf("\n sub of two no =%f",d);printf("\n mul of two no =%f",e);printf("\n div of two no =%f",f1);

}

Page 90: BOOK on C++ Programs

// Program to find the revers of the no.

#include<stdio.h>#include<conio.h>void main(){float p,q;printf("enter any no.");scanf("%f",&p);q=1/p;printf("the revers of the no. is=%f",q);}

Page 91: BOOK on C++ Programs

//sum of 100 no.

#include<stdio.h>void main(){int i,sum=0;for (i=1;i<=100;i++ ){ sum=sum+i; } printf("%d",sum); }

Page 92: BOOK on C++ Programs

//calaulate the sum of series of even no.having last term 100

#include<stdio.h>void main(){ int i,sum=0; for(i=0;i<=100;i=i+2) {sum+=i; } printf("%d",sum); }

Page 93: BOOK on C++ Programs

//find the factorial of the given no.

#include<stdio.h>void main(){int i,ans=1,n;printf("enter the no.");scanf("%d",&n);for (i=1;i<=n;i++ ){ ans=ans*i; } printf("%d",ans); }

Page 94: BOOK on C++ Programs

//find the revers of the no.

#include<stdio.h>#include<conio.h>void main(){float p,q;printf("enter any no.");scanf("%f",&p);q=1/p;printf("the revers of the no. is=%f",q);}

Page 95: BOOK on C++ Programs

//change the temp into forn.

#include<stdio.h>#include<conio.h>void main(){clrscr();float c,f;printf("the value of temp.in cg.");scanf("%f",&c);f=(1.8*c)+32;printf("\n the value of temp. in forenhite=%f",f);}

Page 96: BOOK on C++ Programs

//calculate the discount

#include<stdio.h>#include<conio.h>void main(){float n,p;printf("enter the purching money");scanf("%f",&n);if(n>2000){p=(n*.15);printf("the dis vcount is=%f",p);}elseprintf("the discount is not allowable");}

Page 97: BOOK on C++ Programs

//w.r.t.prog.to read marks of 10 student and store them unde array

#include<iostream.h>#include<conio.h>main(){clrscr();int num[10];int i;cout<<"enter the 10 student marks";for(i=0;i<10;i++){cin>>num[i];}for(i=0;i<10;i++){cout<<"marks of 10 student"<<i+1<<"student"<<num[i];cout<<endl;}getch();}

Page 98: BOOK on C++ Programs

// Program to calculate the area and perimeter of a rectangle

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){int a,b,ar,p;clrscr();cout<<"enter the values of a,b";cin>>a>>b;ar=a*b;cout<<"area="<<ar;p=2*(a+b);cout<<"\n perimeter="<<p;getch();

}

Page 99: BOOK on C++ Programs

// Program to check whether the no. is positive,negative or zero

#include<stdio.h>#include<conio.h>void main(){clrscr();int a;printf("enter the no.");scanf("%d",&a);if(a==0){printf("\n the given no is zero");}elseif(a>0)printf("a is positve ");elseprintf("a is negative");}

Page 100: BOOK on C++ Programs

// Program to find the greatest among 3 number.

#include<stdio.h>#include<conio.h>void main(){clrscr();int a,b,c;printf("enter the three no.");scanf("%d%d%d",&a,&b,&c);if(a>b && a>c)printf("the 1st no. is the greatest no.");elseif(b>a && b>c)printf("the 2nd no. is the greatest no.");elseprintf("the 3rd no. is greatest no.");}

Page 101: BOOK on C++ Programs

// Program to change the mass into energy

#include<stdio.h>#include<conio.h>#include<math.h>void main(){clrscr();float m,e,c=3e8;printf("the mass of the body is");scanf("%f",&m);e=m*pow(c,2);printf("the energy stored in the body is=%e",e);}

Page 102: BOOK on C++ Programs

//A Program to find weather the number is even or odd

#include<stdio.h>#include<conio.h>void main(){clrscr();int a,b;printf("enter the number");scanf("%d",&a);b=a%2;if(b==0){printf("the number is even");}else{printf("the nuber is odd");}getche();}

Page 103: BOOK on C++ Programs

// Program to find the revers of the number.

#include<stdio.h>#include<conio.h>void main(){float p,q;printf("enter any no.");scanf("%f",&p);q=1/p;printf("the revers of the no. is=%f",q);}

Page 104: BOOK on C++ Programs

// Program to print the prime numbers between 1 to 100.

#include<stdio.h>#include<conio.h>void main(){int i,j,d,p=1;for(j=2;j<=100;j++)p=1;for(i=2;i<j;i++){d=j%i;if(d==0){p=0;break;}}if(p==1)printf("%d",j);

}

Page 105: BOOK on C++ Programs

// Program to calculate the lenth of string.

#include<stdio.h>#include<string.h>void main(){char str[15]="Moradabad";int c;c=strlen(str) ;printf("%d",c);}

Page 106: BOOK on C++ Programs

// Program to find the area and volume of sphare.#include<stdio.h>#include<conio.h>void main(){float r,a,v;printf("enter the radius of the sphare");scanf("%f",&r);a=4*3.14*r;v=1.33*3.14*r*r*r;printf("\n area of the sphare is=%f",a);printf("\n volume of the sphare=%f ",v);getch();}

Page 107: BOOK on C++ Programs

// Program to calculate the value of e in the equ. e=m*c*c.

#include<stdio.h>void main(){double e,c=3e8;int m;printf("enter the mass of the substance ");scanf("%d",&m);e=m*c*c;printf("energy of the sys.=%e",e);}

Page 108: BOOK on C++ Programs

// Program to find area and circumference of circle.

#include<stdio.h>#include<conio.h>void main(){float r,a,d;printf("enter the radius of the circle");scanf("%f",&r);a=3.14*r*r;d=2*r;printf("\n area of the circle is= %f",a);printf ("\n dia of the circle is= %f",d);getch();}

Page 109: BOOK on C++ Programs

// Program to find the area and volume of sphare.

#include<stdio.h>#include<conio.h>void main(){float r,a,v;printf("enter the radius of the sphare");scanf("%f",&r);a=4*3.14*r;v=1.33*3.14*r*r*r;printf("\n area of the sphare is=%f",a);

printf("\n volume of the sphare=%f ",v);getch();}

Page 110: BOOK on C++ Programs

// Program to print prime numbers upto 100.

#include<stdio.h>void main(){int i,j,d,p=1; for(j=2;j<=100;j++){ p=1; for(i=2;i<j;i++) { d=j%i; if(d==0) { p=0 ; break; } }if(p==1) printf("%d,",j);

}}

Page 111: BOOK on C++ Programs

//PROGRAM TO FIND OUT THE DAYS OF THE WEEKS

#include<iostream.h>#include<conio.h>void series(int n);void main (){clrscr();int k;cout <<"enter the last limit";cin>>k;series(k);}void series(int n){int i,s=0;for(i=1;i<=n;i++)

s=s+i;cout<<s,endl;getch ();}

Page 112: BOOK on C++ Programs

//PROGRAM TO FIND THE SUM OF LEFT DIAGONAL OF 3*3 MATRIX

#include <iostream.h>#include<conio.h>void main(){clrscr();int a[3][3],i,j,s=0;for(i=0;i<3;i++){for(j=0;j<3;j++){cout<<"enter the value";cin>>a[i][j];}}for(i=0;i<3;i++){for(j=0;j<3;j++){if(i==j)s=s+a[i][j];}}cout<<"sum of left digonal s="<<s;}

Page 113: BOOK on C++ Programs

// PROGRAM TO FIND THE SUM OF RIGHT DIAGONAL

#include<iostream.h>#include<conio.h>void main(){clrscr();int a[3][3],i,j,s=0;for(i=0;i<3;i++){for(j=0;j<3;j++){cout<<"enter the value";cin>>a[i][j];}}for(i=0;i<3;i++){for(j=0;j<3;j++){if(i+j==2){s=s+a[i][j];}}}cout<<"sum of right diagonal s="<<s;}

Page 114: BOOK on C++ Programs

// PROGRAM TO PRINT THE SUM OF GIVEN SERIES//(1)+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+5)+.......up to n

#include<iostream.h>#include<conio.h>void main (){clrscr();int n,c=0,k=0,s=0,r=0;cout<<"enter the value of n";cin>>n;while(c<n){c=c+1;k=0;s=0;while(k<c){k=k+1;s=s+k;}r=r+s;}cout<<r;}

Page 115: BOOK on C++ Programs

// PROGRAM TO FIND OUT THE SUM OF THE N NUMBERS

#include<iostream.h>#include<conio.h>void series (int n);void main(){clrscr();int k;cout<<"enter the last limit";cin>>k;series (k);}void series (int n){int i,s=0;for (i=1;i<=n;i++)s=s+i;cout<<s;getch();}

Page 116: BOOK on C++ Programs

// Program to calculate the area and perimeter of a rectangle

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){int a,b,ar,p;clrscr();cout<<"enter the values of a,b";cin>>a>>b;ar=a*b;cout<<"area="<<ar;p=2*(a+b);cout<<"\n perimeter="<<p;getch();

}

Page 117: BOOK on C++ Programs

// Program which will ask you to enter the radious of a circleand find its area and surfarAnce

#include<iostream.h>#include<conio.h>void main(){clrscr();float r,area=0, cir;cout<<"please enter the radius of a circle";cin>>r;area=3.14*r*r;cout<<endl<<"the area of a circle is"<<area;cir=2*3.14*r;cout<<endl<<"the cir of a circle is"<<cir;getch();}

Page 118: BOOK on C++ Programs

// Program to caculate the area of a room using structures.

#include<iostream.h>#include<conio.h>struct distance{int fit;float inch;};struct room{distance length;distance width;};main(){clrscr();room din;float l,w,area=0;din.length.fit=10;din.length.inch=5;din.width.fit=8;din.width.inch=8;l=din.length.fit+din.length.inch/12;w=din.width.fit+din.width.inch/12;area=l*w;cout<<"the area of a dirty room is ================";cout<<area;getch();

}

Page 119: BOOK on C++ Programs

//Program ENTER ANY INTEGER NO FROM THE KEYBOARD AND FIND WEATHER the number entered is ARMSTORNG or not.

#include<iostream.h>#include<conio.h>void main(){clrscr();int n, i=1,c,x,sum=0,num;cout<<"enter any no";cin>>n;num=n;while(n>0){x=n%10;c=x*x*x;sum=c+sum;n=n/10;}if(num==sum)cout<<"\nnois armstrong no";elsecout<<"\nno is not arms";getch();

}

Page 120: BOOK on C++ Programs

// Program to arrange a list of given nos. in ascending order using array.

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){int list[100],i,j,c,n;clrscr();printf("enter size");scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&list[i]);for(i=0;i<n;i++){

for(j=i+1;j<n;j++){

if(list[i]>list[j]){ c=list[i];

list[i]=list[j];list[j]=c;

}}

}printf("array is started from");for(i=0;i<n;i++)printf("%d ",list[i]);getch();}

Page 121: BOOK on C++ Programs

//Program to print the sum of two arrays.

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){int i,a,l[100],m[100],n[100];clrscr();printf("enter the size of array");scanf("%d ",&a);for (i=0;i<a;i++)scanf("%d",&l[i]);for (i=0;i<a;i++)scanf("%d",&m[i]);for (i=0;i<a;i++)n[i]=l[i]+m[i];for (i=0;i<a;i++)printf(" %d",n[i]);getch();}

Page 122: BOOK on C++ Programs

// Program to declar array.

#include<iostream.h>#include<conio.h>main(){clrscr();int i, num[5];for(i=0;i<=5;i++){cout<<"enter no.";cin>>num[i];}int sum=0;for(i=0;i<=5;i++){sum=sum+num[i];}cout<<"the sum ofsix number is-.-.-.-.-.-.-.-.-.-. "<<sum;getch();}

Page 123: BOOK on C++ Programs

// Program to show the use of integer array using pointer.

#include<iostream.h>#include<conio.h>void main(){clrscr();int arr[]={10,20,30,40,50};int *ptr;ptr=arr;cout<<arr[0];cout<<*ptr<<endl;ptr++;cout<<*ptr<<endl;ptr--;cout<<*ptr;getch();}

Page 124: BOOK on C++ Programs

// Program to declar an array.

#include<iostream.h>#include<conio.h>main(){clrscr();int i, num[5]={1,2,3,4,5};int sum=0;for(i=0;i<=5;i++){sum=sum+num[i];}cout<<"the of five number is "<<sum;getch();}

Page 125: BOOK on C++ Programs

// PROGRAM TO FIND OUT AREA AND PERIMETER OF RECTANGLE

#include <iostream.h>void main(){int a,b,ar,pr;cout<<"enter first side";cin>>a;cout<<"enter second side";cin>>b;ar=a*b;pr=2*(a+b);cout<<"area of rectangle"<<ar<<"\n";cout<<"perimeter of rectangle"<<pr<<"\n";}

Page 126: BOOK on C++ Programs

// PROGRAM TO PRINT THE SUM OF THE EVEN NOS. UPTO ANY NO.

#include<iostream.h>#include<conio.h>void series (int n);void main(){clrscr();int k;cout<<"enter the last limit";cin>>k;series (k);}void series (int n){int i,s=0;for (i=1;i<=n;i++)s=s+i;cout<<s;getch();}

Page 127: BOOK on C++ Programs

// Program to print a piramid of 1

# include <iostream.h>#include<conio.h>void main(){clrscr();long int n,c;n=0,c=0;k:c=c+1;n=(n*10)+1;cout<<n<<"\n";if(c<10) goto k;}

Page 128: BOOK on C++ Programs

// Program to input and print the content of a matrix.

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j;int a[3][3],b[3][3];cout<<"Enter the numbers of the matrix."<<endl;for(i=1;i<=3;i++){for(j=1;j<=3;j++){cin>>a[i][j];}}for(i=1;i<=3;i++){for(j=1;j<=3;j++){b[j][i]=a[i][j];}}for(i=1;i<=3;i++){for(j=1;j<=3;j++){cout<<b[i][j];}cout<<endl;}getche();}

Page 129: BOOK on C++ Programs

// Program to print the name of the week by inputting its no.

#include<iostream.h>#include<conio.h>void main(){int day;cout<<"Enter any day of a week";cin>>day;switch(day){case 1:cout<<"Sunday"<<endl;break;case 2:cout<<"Monday"<<endl;break;case 3:cout<<"Tuesday"<<endl;break;case 4:cout<<"Wednewsday"<<endl;break;case 5:cout<<"Thusday"<<endl;break;case 6:cout<<"Friday"<<endl;break;case 7:cout<<"SATAURDAY"<<endl;break;}}

Page 130: BOOK on C++ Programs

// PROGRAM TO PRINT THE SUM OF THE NATURAL NOS. UPTO ANY NO.

#include<iostream.h>#include<conio.h>void series (int n);void main(){int k;cout<<"Enter the last limit "<<endl;cin>>k;series (k);}void series (int n){int i,s=0;for (i=1;i<=n;i++)s=s+i;cout<<"Sum= "<<s<<endl;getch();}

Page 131: BOOK on C++ Programs

// Program to find the FACTORIAL OF FIRST 10 NUMBERS

#include <iostream.h>void main (){int a=0,s,f=1,b=1;while (a<10){a=a+1;b=0;s=1;while (b<a){b=b+1;s=s*b;}f=f+s;}cout<<"The factorial of first 10 terms is :\n";cout<<f<<endl;cout<<"COMPILED BY- ARVIND GABA"<<"\n";}

Page 132: BOOK on C++ Programs

//PROGRAM TO PRINT THIS GIVEN DESIGN

HAPPY-HAPPYHAPPY HAPPYHAPP APPYHAP PPYHA PYH YHA PYHAP PPYHAPP APPYHAPPY HAPPYHAPPY-HAPPY

#include<iostream.h>#include<conio.h>#include<string.h>#include<stdio.h>#include<iomanip.h>void main(){char *a[11]={"H","A","P","P","Y","-","H","A","P","P","Y"};int j,i,c=5,b=5,d=0;clrscr();for (j=0;j<6;j++){for(i=0;i<=b;i++)cout<<a[i];for(int k=1;k<d;k++)cout<<" ";for (i=c;i<11;i++)cout<<a[i];cout<<"\n";b--;c++;d=d+2;}b=1;c=9;d=7;for(j=0;j<4;j++){for(i=0;i<=b;i++)cout<<a[i];for (int k=d;k>0;k--)cout<<" ";for(i=c;i<11;i++)cout<<a[i];cout<<"\n";b++;c--;d=d-2;}for(i=0;i<11;i++)cout<<a[i];getch();}

Page 133: BOOK on C++ Programs

// PROGRAM TO FIND THE TABLE OF N NUMBER

#include <iostream.h>void main (){int n,a=0,b;cout <<"Enter the value of n "<<endl;cin>>n;cout <<"HERE IS THE TABLE OF "<<n<<"\n";while (a<10){a=a+1;b=n*a;cout<<n<<"*"<<a<<"="<<b<<"\n";}cout<<"COMPILED BY- ARVIND GABA"<<"\n";}

Page 134: BOOK on C++ Programs

//PROGRAM TO FIND OUT THE AVERAGE OF THE NUMBERS

#include <iostream.h>#include<conio.h>void main (){int a[5],i,s=0,avg;for (i=0;i<5;i++){cout<<"Enter the nos. to whome U want to take the AVG. of... \n";cin>>a[i];}for (i=0;i<5;i++){s=s+a[1];}avg =s/5;cout<<"The average of the nos. U have entered is: "<<avg<<"\n";cout<<"COMPILED BY- ARVIND GABA"<<"\n";getch();}

Page 135: BOOK on C++ Programs

//PROGRAM TO ARRANGE THE NUMBERS IN ASSENDING ORDER

#include<iostream.h>void main (){int a[7],i,b,c;for (i=0;i<7;i++){cout <<"Enter the no. \n";cin>>a[i];}for (i=0;i<6;i++){for (b=i+1;b<7;b++){if (a[i]>a[b]){c=a[i];a[i]=a[b];a[b]=c;}}}cout <<"Here R the nos. in ascending order:\n";for (i=0;i<7;i++){cout<<a[i]<<"\n";}cout <<"Compiled by- ARVIND GABA\n";

}

Page 136: BOOK on C++ Programs

//PROGRAM TO SORT THE GIVEN NOS IN ASCEDNING ORDER

#include<iostream.h>#include<conio.h>void main (){int a[7],i,b,c;for (i=0;i<7;i++){cout <<"Enter the no. which u want to sort in ascending \n";cin>>a[i];}for (i=0;i<6;i++){for (b=i+1;b<7;b++){if (a[i]>a[b]){c=a[i];a[i]=a[b];a[b]=c;}}}cout <<"Here R the nos. in ascending order:\n";for (i=0;i<7;i++){cout<<a[i]<<"\n";}cout <<"Compiled by- ARVIND GABA\n";getch();

}

Page 137: BOOK on C++ Programs

// Program to find the table of any number

#include<iostream.h>#include<conio.h>void main(){clrscr();int n,count=1;cout<<"enter any number to see any ";cin>>n;for(;count<=10;count=count+1){cout<<count*n;cout<<endl;} getch(); }

Page 138: BOOK on C++ Programs

// PROGRAM TO PRINT THE SUM OF THE NATURAL NOS. UPTO ANY NO.

#include<iostream.h>#include<conio.h>void series (int n);void main(){clrscr();int k;cout<<"enter the last limit";cin>>k;series (k);}void series (int n){int i,s=0;for (i=1;i<=n;i++)s=s+i;cout<<s;getch();}

Page 139: BOOK on C++ Programs

// Program to calculate the basic salary

#include<iostream.h>#include<conio.h>void main(){clrscr();float b_s,da,hra,gross;cout<<" enter the B_s" ;cin>> b_s ;da=(b_s*40)/100;hra=(b_s*10)/100;gross=da+hra+b_s;cout<<"the gross salary is "<<gross;getch();}

Page 140: BOOK on C++ Programs

// Program to except sale of each day of the week and print total sale

and average sale#include<iostream.h>#include<conio.h>main(){clrscr();int num[7] ;int i,sum=0,aver;cout<<"enter the 7 day's sale";for(i=0;i<7;i++){cin>>num[i];sum=sum+num[i];aver=sum/7;}cout<<"print average"<< aver;getch();}

Page 141: BOOK on C++ Programs

// Program to find out the everage of even number between 1 to 20.

#include<iostream.h>#include<conio.h>void main(){clrscr();int x=0,sum=0,aver=0,count=0;while(x<=10){count++;sum=sum+x ;x=x+2;}cout<<sum<<"\n";cout<<count<<"\n";aver=sum/count;cout<<aver;getch();}

Page 142: BOOK on C++ Programs

// Program to show the use of array

#include<iostream.h>#include<conio.h>main(){clrscr();int num[10] ;int i;cout<<"enter the 10 value for num arry";for(i=0;i<5;i++){cin>>num[i]; }for(i=0;i<5;i++){cout<<num[i];cout<<endl;}getch();}

Page 143: BOOK on C++ Programs

// PROGRAM TO PRINT THE SUM OF THE SERIES GIVEN BY THE USER

#include<iostream.h>void main(){int p,n,s=0,c=0,sum;cout<<"enter the ending number of the series";cin>>n;for( p=1;p<=n;p++ )s=s+c/p;cout<<"sum of the series"<<sum;}

Page 144: BOOK on C++ Programs

// Program to calculate the year discount

#include<iostream.h>#include<conio.h>void main(){clrscr();int cyear,jyear,bonus,total ;cout<<"current year value and year of joining";cin>>cyear>>jyear; total=cyear-jyear;if(total> 3)bonus=2500;

cout<<"the bonus will be "<<bonus;getch();}

Page 145: BOOK on C++ Programs

// Program to create structure with array

#include<iostream.h>#include<conio.h>struct book{char bname[12];char bauthor[12];int pages;float price;}b[10];void main(){for(int i=0;i<10;i++){cout<<"enter the info for book"<<i+1<<" ";cout<<"book name is";cin>>b[i].bname;cout<<"enter author name";cin>>b[i].bauthor;cout<<"no of page in a book";cin>>b[i].pages;}for(i=0;i<10;i++){cout<<b[i].bname;cout<<b[i].bauthor;cout<<b[i].pages;}getch();}

Page 146: BOOK on C++ Programs

// Program to calculate temp in celcius and feranite

#include<iostream.h>#include<conio.h>void main(){clrscr();float temp,i=1,c=0;while(i<=3){cout<<"enter temp in farenite";cin>>temp;c=(temp-32)*5/9;cout<<"temp in celcius is ="<<c;cout<<endl;i++;}getch();}

Page 147: BOOK on C++ Programs

// Program to checking the equality of two matrix

#include<iostream.h>#include<conio.h>#include<process.h>void main(void){clrscr();int i,j,x[10][10],y[10][10];int m,n,flag=0,l,k,a,b ,c;cout<<"enter the size of first matrix";cin>>i>>j;

cout<<"enter the size of two metrix";cin>>m>>n;cout<<"ENTER THE MATRIX FIRST";for(l=0;l<i;l++){for(k=0;k<j;k++){cin>>x[l][k];}}

cout<<"ENTER THE MARTIX SECOND";for(a=0;a<m;a++){for(b=0;b<n;b++){cin>>y[a][b];}}if(i!=m && j!=n){ cout<<"not equal"; flag=1; exit(0);}

elsefor( int i1=0;i1<i;i++) { for( int j1=0;j1<j;j1++) { if(x[i1][j1]!=y[i1][j1])

{ flag=1; break;

} }}if(flag==0)cout<<"matrix are equal";elsecout<<"not equal";getch();}

Page 148: BOOK on C++ Programs

// Program enter an string from keyboadr and find out its length

#include<iostream.h>#include<conio.h>main(){char i[100] ;int len ;clrscr();cout<<"enter the character";cin>>i;for(len=0;i[len]!='\0';len++);cout<<"length number of character"<<len;getch();}

Page 149: BOOK on C++ Programs

// Program to copare two strings using strcmp()

#include<iostream.h>#include<conio.h>#include<string.h>main(){ int i;clrscr();char str1[10],str2[10];cout<<"enter the value";cin>>str1;strcpy(str2,str1);cout<<str1;

getch();

}

Page 150: BOOK on C++ Programs

// Program to enter any string from the key board and copy into another string

#include<iostream.h>#include<conio.h>#include<string.h>main(){int len,i;char str1[50],str2[50];cout<<"enter the string";cin>>str1;for(i=0;str1[i]!='\0';i++){str2[i]=str1[i];}str2[i]='\0';cout<<str2;getch();}

Page 151: BOOK on C++ Programs

// Program to perform the corresponding arithmetic operation as given by the user.

#include<iostream.h>#include<conio.h>int main(){clrscr();char ch;float a,b,result;cout<<"enter two numbers";cin>>a>>b;cout<<"\n"<<"enter the operator(+,-,*,/):";cin>>ch;cout<<"\n";if(ch=='+')result=a+b;else if(ch=='-')result=a-b;esleif(ch=='*')result=a*b;esle if(ch=='/')result=a/b;else{cout<<"\n"<<"wrong operator!"<<"\n";gotolb;}cout<<"\n"<<"the calculated result is";<<"\n"<<result<<"\n";lb;return 0;}

Page 152: BOOK on C++ Programs

// Program to enter a side of a cube and find volume and surfacearea

#include<iostream.h>#include<conio.h>void main(){clrscr();float side, vol, area, s_area;cout<<"please enter the side of a cube";cin>>side;vol=side*side*side;s_area=(6*side);cout<<"the vol of cube is"<<vol;cout<<"the s area is"<<s_area;getch();}

Page 153: BOOK on C++ Programs

// Program to print the corresponding name of week

#include<iostream.h>#include<conio.h>int main(){clrscr();int d_o_w;cout<<"enter number of week's day(1-7)";cin>>d_o_w;switch(d_o_w){case 1:cout<<"\nsunday";

break;case 2:cout<<"\nmonday";

break;case 3:cout<<"\ntuesday";

break;case 4:cout<<"\nwednesday";

break;case 5:cout<<"\nthursday";

break;case 6:cout<<"\nfriday";

break;case 7: cout<<"\nsaturday";

break;default:cout<<"\wrong number of day!";}return 0;}

Page 154: BOOK on C++ Programs

// Program to show the use of default parameter

#include<iostream.h>#include<conio.h>void main(){clrscr();func(10,20);getch();}void func(int a,int b,int dog=10){cout<<a<<endl<<b<<’\n’<<dog;}

Page 155: BOOK on C++ Programs

// Program to print the following output

******************************* ******************* ***************** *************** ************* *********** ********* ******* ***** *** *** ***** ******* ********* *********** ************* *************** ***************** ******************* *******************************

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){int a,b,c;clrscr();a=11;b=-1;c=10;for(int i=1;i<=11;i++){ for(int o=1;o<=25;o++)

printf(" ");for(int j=1;j<=a;j++)

printf("*");for(int k=1;k<=b;k++)

printf(" ");for(int l=1;l<=c;l++)

printf("*");printf("\n");if(i!=1)c--;a--;b+=2;

}a=2;b=17;c=2;

for( i=1;i<=10;i++){

for(int o=1;o<=25;o++)printf(" ");

Page 156: BOOK on C++ Programs

for(int j=1;j<=a;j++)printf("*");

for(int k=1;k<=b;k++)printf(" ");

for(int l=1;l<=c;l++)printf("*");

printf("\n");

a++;if(i!=10)b-=2;if(i!=9)c++;

}getch();}

Page 157: BOOK on C++ Programs

// Program to find the area and volume of sphare

#include<stdio.h>#include<conio.h>void main(){float r,a,v;printf("enter the radius of the sphare");scanf("%f",&r);a=4*3.14*r;v=1.33*3.14*r*r*r;printf("\n area of the sphare is=%f",a);

printf("\n volume of the sphare=%f ",v);getch();}

Page 158: BOOK on C++ Programs

// Program which will ask u to enter diastance between to city in kilometer and find its equal distance in m, cm

#include<iostream.h>#include<conio.h>void main(){clrscr();float dis, meter,cent;cout<<"please enter the distance";cin>>dis;meter=1000*dis;cent=meter*100;cout<<"distance in meteris"<<meter;cout<<"distance in centemeter is"<<cent;getch();}

Page 159: BOOK on C++ Programs

// A Program to Print the Area of all the Shapes

#include<stdio.h>#include<conio.h>void main(){int a,b,t,s,g;float r,o;clrscr();printf("inter the values of a,b,r");scanf("%d%d%f",&a,&b,&r);t=0.5*a*b;s=a*a;g=a*b;o=3.14*r*r;printf("\n the area of triangle=%d",t);printf("\n the area ofsquare =%d",s);printf("\n the area of rectangle=%d",g);printf("\n the area of circle=%f",o);getch();}

Page 160: BOOK on C++ Programs

// Program to find the sum of 100 number.

#include<stdio.h>#include<conio.h>void main(){clrscr();\int i,sum=0;for (i=1;i<=100;i++ ){ sum=sum+i; } printf("%d",sum); }

Page 161: BOOK on C++ Programs

// Program to calaulate the sum of series of even no.having last term 100

#include<stdio.h>#include<conio.h>void main(){ clrscr(); int i,sum=0; for(i=0;i<=100;i=i+2) {sum+=i; } printf("%d",sum); }

Page 162: BOOK on C++ Programs

// Program to find the factorial of the given no.

#include<stdio.h>#include<conio.h>void main(){clrscr();int i,ans=1,n;printf("enter the no.");scanf("%d",&n);for (i=1;i<=n;i++ ){ ans=ans*i; } printf("%d",ans); }

Page 163: BOOK on C++ Programs

// Program to find the revers of the no.

#include<stdio.h>#include<conio.h>void main(){clrscr();float p,q;printf("enter any no.");scanf("%f",&p);q=1/p;printf("the revers of the no. is=%f",q);}

Page 164: BOOK on C++ Programs

// Program to find the greater number from two number.

#include<stdio.h>#include<conio.h>void main(){clrscr();int a,b;printf("enter the two values");scanf("%d%d",&a,&b);if(a>b)printf("a is greater than b");elseprintf("\n b is greater than a");getch();}

Page 165: BOOK on C++ Programs

// Program to find area and circumference of circle

#include<stdio.h>#include<conio.h>void main(){clrscr();float r,a,d;printf("enter the radius of the circle");scanf("%f",&r);a=3.14*r*r;d=2*r;printf("\n area of the circle is= %f",a);

printf ("\n dia of the circle is= %f",d);getch();}

Page 166: BOOK on C++ Programs

// Program to gave a discount for such item

#include<iostream.h>#include<conio.h>void main(){clrscr();int qty,dis=0;float price,total;cout<<"please enter the quantity and price";cin>>qty>>price;if(qty>100);dis=10;total=(qty*price)-(qty*price*dis)/100;cout<<"total expence"<<total;getch();}

Page 167: BOOK on C++ Programs

// PROGRAM TO PRINT THE GIVEN SERIES

11212312341234512345612345671234567812345678912345678910

#include<iostream.h>void main(){int i,j;for(i=1;i<=10;i++){for(j=1;j<=i;j++){cout<<j;}cout<<"\n";}}

Page 168: BOOK on C++ Programs

// Program to show the use of endl manipulator

#include<iostream.h>#include<conio.h>void main(){cout<<"welcome"<<endl<<"in tmimt";getch();}

Page 169: BOOK on C++ Programs

#include<iostream.h>#include<conio.h>void main(){ int count=2;count=2;while(count<=20){cout<<count;count=count+2;}getch();}

Page 170: BOOK on C++ Programs

// Program to find the sum of all even and odd numbers in an array.

#include<iostream.h>#include<conio.h>main(){clrscr();int num[5],i,se=0,so=0;cout<<"enter the 5 number";for(i=0;i<5;i++){cin>>num[i];} for(i=0;i<5;i++) {if(num[i]%2==0)se+=i;elseso+=i;}cout<<"the sum of even is:"<<se;cout<<endl<<"the sum of odd is-----" <<so;getch();}

Page 171: BOOK on C++ Programs

// Program enter a int number to find out its factorial

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

int fac(num){int f=1,t;while(num>0){f=f*num;num--;}return(f);}

void main(){int num,e;clrscr();cout<<"Enter the number to get factorial";cin>>num;e=fac(num);cout<<e;getch();}

Page 172: BOOK on C++ Programs

// Program to find out factorial of a number

#include<iostream.h>#include<conio.h>void main(){float num,fact=1;cout<<"please enter any number";cin>>num;while(num>0){fact=fact*num;num--;}cout<<fact;getch();}

Page 173: BOOK on C++ Programs

// Program to print the Febonacci series till n Terms

#include<iostream.h>#include<conio.h>main(){ int x=0,y=1,z=0,count=1,n; clrscr(); cout<<"enter the limit"; cin>>n;

cout<<x<<” “<<y<<” “; while(count<n-2) { z=x+y; cout<< z; x=y ; y=z; count++; } getch(); }

Page 174: BOOK on C++ Programs

// Program to make a design with ‘@’ sign

#include<iostream.h>#include<conio.h>void main(){clrscr();int r,c;for(r=1;r<=25;r++){cout<<endl;for(c=1;c<=r;c++){cout<<"@"<<" ";}}getch();}

Page 175: BOOK on C++ Programs

// PROGRAM TO PRINT THE GIVEN DESIGN*************************

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

void main(){clrscr();int i,j;for (i=1;i<=5;i++){for (j=1;j<=i;j++){cout<<"*";}cout<<"\n";}for (i=4;i>=1;i--){for (j=1;j<=i;j++){cout<<"*";}cout<<"\n";}}

Page 176: BOOK on C++ Programs

// Program to show the use of for loop

#include<iostream.h>#include<conio.h>void main(){clrscr();cout<<"please see the number ";int i;for(i=1;i<10;i++)cout<<i;cout<<endl;getch();}

Page 177: BOOK on C++ Programs

// Program to show the use of setw() function

#include<iostream.h>#include<conio.h>#include<iomanip.h>void main(){clrscr();cout<<"welcome"<<setw(5)<<"in"<<endl<<"moradabad";getch();}

Page 178: BOOK on C++ Programs

// Program to copy one Pointer string to another using user defined function

#include<iostream.h>#include<conio.h>void main(){clrscr();char str1[]="abcd",str2[20];void copy(char*,char*);copy(str1,str2);cout<<str2;getch();}void copy(char *s,char *p){while(*s!='\0'){ *p=*s;p++;s++;}*p='\0';}

Page 179: BOOK on C++ Programs

// Program to find the sum using two numbers

#include<iostream.h>#include<conio.h>add(int n1,int n2){int sum=0;sum=n1+n2;return(sum);

}void main(){clrscr();int num1,num2 ,d ;cout<<"enter two number ";cin>>num1>>num2;d=add(num1,num2);cout<<"the total is ---"<<d;getch();}

Page 180: BOOK on C++ Programs

// Program to enter two numbers and find the greatest one using function

#include<iostream.h>#include<conio.h>int comp(int,int);void main(){int num1,num2,d;cout<<"enter two value";cin>>num1>>num2;d=comp(num1,num2);cout<<d;getch();}int comp(int n1,int n2){if(n1>n2)return(n1);elsereturn(n2);

}

Page 181: BOOK on C++ Programs

// Program to convert Hexadecimal into decimal number system

#include<iostream.h>#include<conio.h>#include<stdio.h>#include<math.h>#include<string.h>void main(){char num[8]={0};int i,l,r,s=0,c;clrscr();printf("Enter a Hexadecimal No. ");gets(num);l=strlen(num);c=0;for(i=l-1; i>=0; i--){

if(num[i]<=57)r=num[i]-48;

elser=num[i]-55;

s=s+r*pow(16,c);c++;

}printf("The Decimal is %d",s);getch();}

Page 182: BOOK on C++ Programs

// Program to check whether a given no. is +ve,-ve or zero

#include<process.h>#include<conio.h>#include<stdio.h>void main(){int a;clrscr();printf("enter the value of a");scanf("%d",&a);if (a<0){printf("negative");}if (a>0){

printf("positive");}if (a=0){printf("zero");}getch();}

Page 183: BOOK on C++ Programs

// Program to check whether a given no. is prime or not

#include<conio.h>#include<stdio.h>#include<math.h>void main(){int i,x,c;clrscr();c=0;cin>>x;for(i=2;i<=x/2;i++){if(x%i==0){c=1;break;}elsec=0;}if (c==1)cout<<"not prime";elsecout<<"prime";getch();}

Page 184: BOOK on C++ Programs

// Program to replace every space in a string with hyphen

#include<iostream.h>#include<conio.h>#include<string.h>main(){clrscr();char str[50],i,len=0;cout<<"enter the string";cin.getline(str,50);//len=strlen(str);for( i=0;i<50;i++){ if(str[i]==' ')str[i]='-';}//strfor(i=0;i<str[i]!='\0';i++)cout<<str[i];

getch();}

Page 185: BOOK on C++ Programs

// Program to enter the four side of a rectangle and find out weather its area is greater than its perimeter

#include<iostream.h>#include<conio.h>void main(){clrscr();int a,b,area,per;cout<<"please enter the value of a";cin>>a;cout<<"please enter the value of b";cin>>b;area=a*b;per=2*(a+b);if (area>per){cout<<"area is greater than per"<<area ;}elsecout<<"area is less than per";getch(); }

Page 186: BOOK on C++ Programs

// Program for the sum of digit of an int number

#include<iostream.h>#include<conio.h>void main(){clrscr();int num,sum=0,n;cout<<"please enter any integer number";cin>>num;while(num>0){n=num%10;sum=sum+n;num=num/10;}cout<<sum;getch();}

Page 187: BOOK on C++ Programs

// PROGRAM TO PRINT THE GIVEN SERIES122333444455555

#include<iostream.h>#include<conio.h>void main(){int i,j;clrscr();for(i=1;i<=5;i++){for(j=1;j<=i;j++){cout<<i;}cout<<"\n";}}

Page 188: BOOK on C++ Programs

// Program to print the given design11212312341234512345612345671234567812345678912345678910

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j;for(i=1;i<=10;i++){for(j=1;j<=i;j++){cout<<j;}cout<<"\n";}}

Page 189: BOOK on C++ Programs

// Program to use nested structure for period of work of an employee

#include<iostream.h>#include<conio.h>struct date{int dd;int mm;int yyyy;};struct emp{char name[10];int emp_no;float sal;date d;date cd;};void main(){clrscr();emp e;int time,d,cd;cout<<"enter the empolyee name";cin>>e.name;cout<<"enter the empolyee number";cin>>e.emp_no;cout<<"enter the empolyee salary";cin>>e.sal;cout<<"enter the date of joining ";cin>>e.d.dd>>e.d.mm>>e.d.yyyy;e.cd.dd=19;e.cd.mm=11;e.cd.yyyy=2001;time=e.cd.yyyy-e.d.yyyy;if(time>10)cout<<"the empolyee is permanent ";elsecout<<"the empolye is not permanent";getch();}

Page 190: BOOK on C++ Programs

// Program to make computer play the KBC game with the user

#include<iostream.h>#include<conio.h>#include<dos.h>#include<string.h>#include<stdio.h>void design();void input();void process();void main(){clrscr();int c=0;design();input();process();if(c!=5){clrscr();textcolor(4);gotoxy(40,12);cprintf("SORRY WRONG ANSWER");getch();}}void design(){cout<<'\a';int i,j;for(i=0;i<320;i++){textcolor(1);cprintf ("*");}for(j=0;j<25;j++){textcolor(4);gotoxy(1,j);cprintf("*");textcolor(4);gotoxy(80,j);cprintf("*");delay(50);}delay(100);textcolor(2);gotoxy(30,12);cprintf("KAUN BANEGA GA CROREPATI");}void input(){textcolor(14);gotoxy(30,14);cprintf("COMPUTER BACHHAN WILL ASK YOU 5 QUESTION");textcolor(14);gotoxy(30,15);

Page 191: BOOK on C++ Programs

cprintf("INPUT THE CORRECT CHOICE");getch();}void process(){int ch;int c;clrscr();textcolor(4);gotoxy(20,7);cprintf("QUESTION NO 1:");textcolor(1);gotoxy(20,10);cprintf("NAME A COUNTRY WITH UNWRITTEN CONSTITUTION");textcolor(1);gotoxy(20,12);cprintf("(a) INDIA (b)USA (c)RUSSIA (d)ENGLAND");cout<<endl;cin>>ch;if(ch!=4){goto k;}else{c=c+1;textcolor(4);gotoxy(20,7);cprintf("QUESTION NO 2:");textcolor(1);gotoxy(20,12);cprintf("INDIAS SUPER COMPUTER IS");textcolor(1);gotoxy(20,12);cprintf("(a)IBM (b) PARAM 20000 (c)CSIR (d)P 1000");cin>>ch;}if(ch!=2){goto k;}else{clrscr();c=c+1;textcolor(4);gotoxy(20,7);cprintf("QUESTION NO 3:");textcolor(1);gotoxy(20,10);cprintf("FIRST INDIAN MISS WORLD");textcolor(1);gotoxy(20,12);cprintf("(A) SUSHMITA SEN (B) PRIYANKA CHOPRA (C) ASHIWARYA RAI (D) RITA FARIA");cin>>ch;}

Page 192: BOOK on C++ Programs

if(ch!=4){goto k;}else{c=c+1;textcolor(1);gotoxy(20,7);cprintf("QUESTION NO 4:");textcolor(1);gotoxy(20,10);cprintf("AMITABH BACHHANS HOUSE IS KNOWN AS");textcolor(1);gotoxy(20,12);cprintf("(A) SEA WINGS (B) KAMMANA (C)PRATIKSHA (D) ABCL HOUSE");cin>>ch;}if(ch!=3){goto k;}else{c=c+1;textcolor(4);gotoxy(20,7);

cprintf(" QUESTION NO 5");textcolor(1);gotoxy(20,10);cprintf("IN 1736 WHICH CENTURY WAS IT");textcolor(1);gotoxy(20,12);cprintf("(A)18 (B) 12 (C) 56 (D) 78");cin>>ch;}if(ch!=1)c=c+1;k:

}

Page 193: BOOK on C++ Programs

// PROGRAM TO CONVERT DECIMAL TO BINARY

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){clrscr();int i,m,c[100];printf("enter any number");scanf("%d",&m);i=0;do{c[i]=m%2;m=m/2;i++;}while(m!=0);i--;do{

printf("%d",c[i]);i--;}while(i!=-1);getch();}

Page 194: BOOK on C++ Programs

// PROGRAM TO PRINT THEGIVEN SERIES*******************************************************

#include<iostream.h>void main(){int i,j;for(i=1;i<=10;i++){for(j=1;j<=i;j++){cout<<"*";}cout<<"\n";}}

Page 195: BOOK on C++ Programs

// PROGRAM TO PRINT THE PASCAL'S TRIANGLE

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j,k,n=40,l;for(i=1;i<=9;i++){for(j=1;j<=n;j++){cout<<" ";}for(k=1;k<=i;k++){cout<<k;}for(l=(i-1);l>=1;l--){cout<<l;}cout<<endl;n=n-1;}}

Page 196: BOOK on C++ Programs

// Program to print the given design

#include<iostream.h>#include<conio.h>void main(){int i=1;cout<<endl<<"----------";while(i<=10){cout<<endl<<"| |";cout<<endl;i++;}cout<<endl<<"----------";getch();}

Page 197: BOOK on C++ Programs

// Program to calculate the LCM, & HCF

#include<iostream.h>#include<conio.h>main(){ int n1,n2,x,y,i,gcd=0,lcm=1; cout<<"enter 2 numbers."; cin>>n1>>n2; x=n1; y=n2; for(i=2;i<100;i++) { lb: if((n1%i==0)||(n2%i==0))

lcm=lcm*i; if(n1%i==0) n1=n1/i; if(n2%i==0) n2=n2/i; if((n1%i==0)||(n2%i==0)) goto lb; if((n1==1)&&(n2==1)) break; } gcd=x*y/lcm; cout<<"LCM= "<<lcm<<"\n"<<"GCD= "<<gcd; getch(); return(0); }

Page 198: BOOK on C++ Programs

// Program to enter two string then compares them without string function strcmp()

#include<iostream.h>#include<conio.h>#include<string.h>#include<process.h>main(){int flag=0;char str1[10],str2[10];cout<<"enter the two string";cin>>str1>>str2;

int len1=strlen(str1);int len2=strlen(str2);if(len1!=len2){cout<<"not equal";exit(0);}elsefor(int i=0;i<len2-1;i++){clrscr();if(str1[i]!=str2[i]){flag=1;break;}{if(flag==1)cout<<"not equal";elsecout<<"equal";}}getch();}

Page 199: BOOK on C++ Programs

// PROGRAM TO PRINT A WELCOME MESSAGE ON SCREEN

#include<iostream.h>void main (){cout<<"Welcome to C++"<<endl;}

Page 200: BOOK on C++ Programs

// PROGRAM TO FIND OUT THE GREATEST OF 3 NUMBERS.

#include <iostream.h> void main ( ) { int x,y,z,g; cout<<"\n enter first number:"; cin>>x; cout<<"\n enter second number:"; cin>>y; cout<<"\n enter third number:"; cin>>z; if (x>y) g=x; else g=y; if (g>z) cout<<"\n the greatest number is :"<<g; else cout<<"\n the greatest number is:"<<z; }

Page 201: BOOK on C++ Programs

// Program to find area of a triangle

#include<iostream.h>#include<conio.h>#include<stdio.h>#include<math.h>void main(){clrscr();float a,b,c,s,g;cout<<"enter the values of a b c";cin>>a>>b>>c;s=(a+b+c)/2;float f;f=s*(s-a)*(s-b)*(s-c);g=pow(f,1/2);cout<<"the area of triangle ="<<g;getch();}

Page 202: BOOK on C++ Programs

// Program to enter the marks of a student in four sub and fing percentage

#include<iostream.h>#include<conio.h>void main(){clrscr();int a,b,c,d,e,per;cout<<"please enter the marks in four sub";cin>>a>>b>>c>>d;e=a+b+c+d;cout<<"the total is "<<e;

per=(e*100)/400;if (per>=50){cout<<"it is passed welldone my boy ";}else {cout<<"oh,sorry you are failed ,try again";}getch();}

Page 203: BOOK on C++ Programs

// Program to show addition of two 3*3 matrices

#include<stdio.h>#include<conio.h>void main(){clrscr();int a[3][3],b[3][3],c[3][3];

int i=0,j=0,k=0;clrscr();for( i=0;i<3;i++){for( j=0;j<3;j++)scanf("%d",&a[i][j]);}for( i=0;i<3;i++){for( j=0;j<3;j++)scanf("%d",&b[i][j]);}for( i=0;i<3;i++){for(j=0;j<3;j++){printf("%d\t",a[i][j]);}printf("\n");}printf("\n");for(i=0;i<3;i++){for(j=0;j<3;j++){printf("%d\t",b[i][j]);}printf("\n");}for(i=0;i<3;i++)for(j=0;j<3;j++)for( k=0;k<3;k++)c[i][j]=a[i][j]+b[i][j];printf("\n");for(i=0;i<3;i++){for(j=0;j<3;j++){printf("\t%d",c[i][j]);}printf("\n");}getch();}

Page 204: BOOK on C++ Programs

// Program to show the multiplication of two 3*3 matrices

#include<stdio.h>#include<conio.h>void main(){

int a[3][3],b[3][3],c[3][3];

int i=0,j=0,k=0;clrscr();for( i=0;i<3;i++){for( j=0;j<3;j++)scanf("%d",&a[i][j]);}for( i=0;i<3;i++)

{for( j=0;j<3;j++)scanf("%d",&b[i][j]);}for( i=0;i<3;i++){for(j=0;j<3;j++){printf("%d\t",a[i][j]);}printf("\n");}printf("\n");for(i=0;i<3;i++){for(j=0;j<3;j++){printf("%d\t",b[i][j]);}printf("\n");}for(i=0;i<3;i++){for(j=0;j<3;j++){c[i][j]=0;for( k=0;k<3;k++)c[i][j]=c[i][j]+a[i][k]*b[k][j];}}for(i=0;i<3;i++){for(j=0;j<3;j++){printf("\t%d",c[i][j]);}printf("\n");}getch();}

Page 205: BOOK on C++ Programs

// Program to perform the adding of two matrix

#include<iostream.h>#include<conio.h>main(){clrscr();int arr1[2][3],arr2[2][3],arr3[2][3] ,i=0,j ;cout<<"enter the value for array";for( i=0;i<2;i++){for( j=0;j<3;j++){cin>>arr1[i][j];}}

for( i=0;i<2;i++){for( j=0;j<3;j++){cin>>arr2[i][j];}}for( i=0;i<2;i++){for( j=0;j<3;j++){arr3[i][j]=arr1[i][j]+arr2[i][j];}}cout<<"the result of sum of two matrix is =========";for( i=0;i<2;i++){cout<<endl; { for( j=0;j<3;j++)

cout<<arr3[i][j]<<" ";}

getch();}}

Page 206: BOOK on C++ Programs

// Program to find the maximum in a matrix

#include<iostream.h>#include<conio.h>main(){clrscr();int a[2][3],max=0;cout<<"enter the value of matrix";for(int i=0;i<2;i++){ for(int j=0;j<3;j++)cin>>a[i][j];{if(max<a[i][j])max=a[i][j];}

}cout<<max;getch();}

Page 207: BOOK on C++ Programs

// PROGRAM TO PRINT THE AVERAGE OF FIRST 15 NUMBERS ON SCREEN

# include <iostream.h>void main(){int c,s=0;float avg;c=0;k:c=c+1;s=s+c;cout<<c<<"\n";if(c<15) goto k;avg = s/15;cout<<"average="<<avg;}

Page 208: BOOK on C++ Programs

// Program to perform all 4 arithmetic operations on three values

#include<iostream.h> #include<conio.h> #include<math.h> void main() {

clrscr();int a,b,c,sum = 0,difference=0,product=0,quotient=0;for( int i = 1; i<3;i++){cout<<"\n Enter First values";cin>>a;cout<<"\n Enter second values";cin>>b;cout<<"\n Enter Third values";cin>>c;sum = a + b + c;difference = a - b - c;product = a * b * c;quotient = a / b / c;cout<<"\n Sum = "<<sum;cout<<"\n Difference = "<<difference;cout<<"\n product = "<<product;cout<<"\n Quotient = "<<quotient;getch();}

}

Page 209: BOOK on C++ Programs

// Program to perform all 4 arithmetic operations on three values

#include<iostream.h> #include<conio.h> #include<math.h> void main() {

clrscr();int a,b,c,sum = 0,difference=0,product=0,quotient=0;for( int i = 1; i<3;i++){cout<<"\n Enter First values";cin>>a;cout<<"\n Enter second values";cin>>b;cout<<"\n Enter Third values";cin>>c;sum = a + b + c;difference = a - b - c;product = a * b * c;quotient = a / b / c;cout<<"\n Sum = "<<sum;

cout<<"\n Difference = "<<difference;cout<<"\n product = "<<product;cout<<"\n Quotient = "<<quotient;getch();}

}

Page 210: BOOK on C++ Programs

// PROGRAM TO CONVERT DECIMAL TO BINARY

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){clrscr();int i,m,c;cout<<"enter any number";cin>>m;do{c=m%16;m=m/16;if(c<10)cout<<c;if(c==10)cout<<"A";if(c==11)cout<<"B";if(c==12)cout<<"C";if(c==13)cout<<"D";if(c==14)cout<<"E";if(c==15)cout<<"F";}while(m!=0);getch();}

Page 211: BOOK on C++ Programs

// Program to print the given design*******************************************************

#include<iostream.h>void main(){int i,j;for(i=1;i<=10;i++){for(j=1;j<=i;j++){cout<<"*";}cout<<"\n";}}

Page 212: BOOK on C++ Programs

// Program to find the diff. and div. of two real nos.

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){float a,b,s,t;clrscr();printf("enter the values of a,b");scanf("%f %f",&a,&b);s=b-a;printf("%f",s);t=b/a;printf(" %f",t);getch();}

Page 213: BOOK on C++ Programs

// Program to find the divisior

#include<iostream.h>#include<conio.h>void main(){clrscr();int i=1,num;cout<<"la la lo number";cin>>num;for(;i<=num;i++){if(num%i==0)cout<<i<<endl;}getch();}

Page 214: BOOK on C++ Programs

//PROGRAM TO PRINT THE SUM OF TWO GIVEN NOS.

#include<iostream.h>void main(){int a,b,c;a=15;b=20;c=a+b;cout<<"\n print the sum of a+b="<<c;}

Page 215: BOOK on C++ Programs

// Programe to calculate the sum of series

#include <iostream.h>void main(){int i=0,s=0,l=0;start:i=i+1;s=s+1;l=l+1;if(i<=10)goto start;{cout<<"\n sum of series="<<i;}}

Page 216: BOOK on C++ Programs

// program to calculate the sum of series

#include <iostream.h>void main(){int i=0,s=0,l=0;start:i=i+1;s=s+1;l=l+1;if(i<=10)goto start;{cout<<"\n sum of series="<<i;}}

Page 217: BOOK on C++ Programs

// Program to find the number is equal , greater or less than the other number

#include<iostream.h>#include<conio.h>void main(){clrscr();int num1,num2;cout<<"please enter two number";cin>>num1>>num2;if(num1>num2)cout<<"num1 is greater then"<<num1;else{if(num2>num1)cout<<"num2 is ___"<<num2;elsecout<<"is equal"<<num1;}getch();}

Page 218: BOOK on C++ Programs

// Program to show the use of pointer& one dimension array

#include<iostream.h>#include<conio.h>main(){clrscr();char arr[]={'a','b','c','d','\0'};cout<<(unsigned long)arr;cout<<arr[0]<<endl;cout<<endl<<&arr[0];cout<<endl<<*(&arr);cout<<endl<<*(&arr[1]);cout<<endl<<(arr+1);getch();}

Page 219: BOOK on C++ Programs

// Program to print the armstrom number from 10-100

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){clrscr();long int i,d,n,s=0,r,old,p;cout<< "armstrong number fron 10 to 1000 are as follows: \n";for(p=10;p<=1000;p++){ s=0;

old=n=p;do{d=n%10;r=d*d*d;s+=r;n=n/10;}while(n!=0);

if(old==s)cout<<old<<" ";

}getch();}

Page 220: BOOK on C++ Programs

// Program to find out weather the number entered is palindom

#include<iostream.h>#include<conio.h>main(){clrscr();int n,num,digit,rev=0;cout<<"input the number";cin>>num;n=num;do{digit=num%10;rev=rev*10+digit;num=num/10;}while(num!=0);cout<<"the reverse of no"<<endl;cout<<rev<<endl;if(n==rev)cout<<"the number is palindrome";elsecout<<"the no is not pal-----";getch();}

Page 221: BOOK on C++ Programs

// PROGRAM TO PRINT THE SUM OF TWO GIVEN NUMBERS

#include<iostream.h>#include<conio.h>void main (){clrscr();int a,i,b,c;for(i=0;i<5;i++){cout<<"\n Enter two numbers";cin>>a;cin>>b;c=a+b;cout<<"\n sum"<<c;}}

Page 222: BOOK on C++ Programs

// Program to show the use of passing structure variable to function

#include<iostream.h>#include<conio.h>struct student{int rno;char name[20];int marks;};void input(student &);void main(){student abc;input(abc);cout<<"roll number"<<abc.rno;cout<<"NAME OF A STUDENT"<<abc.name;cout<<"MARKS OF THE STUDENT"<<abc.marks;getch();}void input(student & var ){cout<<"ENTER THE ROLL NUMBER";cin>>var.rno;cout<<"ENTER THE NAME ========?";cin>>var.name;cout<<"ENTER THE MARKS AGAR HA NAHI ZERO";cin>>var.marks;}

Page 223: BOOK on C++ Programs

// Programe which will ask you to enter the marks of four subjectdisplay the total and the persantage

#include<iostream.h>#include<conio.h>void main(){clrscr();float a,b,c,d,e;cout<<"enter the marks in four subject";cin>>a>>b>>c>>d;e=a+b+c+d;cout<<"the total is"<<e;float f;f=(e*100)/400;cout<<"the percentage is"<<f;getch();}

Page 224: BOOK on C++ Programs

// Program to enter a no from key board find out weather it is a perfect number or not

#include<iostream.h>#include<conio.h>void main(){clrscr();int i=1, sum=0,num;cout<<"please enter the funny number";cin>>num;while(i<num){if(num%i==0)sum=sum+i;i++;}{if(sum==num)cout<<"perfect";elsecout<<"no not perfect bad luck my boy try again";}getch();}

Page 225: BOOK on C++ Programs

// Program to find the sum of first 10 natural numbers

#include<iostream.h>#include<conio.h>void main(){int n,sum=0;for(n=1;n<=10;n++){sum=sum+n;

}cout<<sum;

getch();}

Page 226: BOOK on C++ Programs

// Program to demstrate the use of POINTER

#include<iostream.h>#include<conio.h>void main(){clrscr();int i=3;cout<<&i<<"\n";//shows the address of icout<<*(&i)<<"\n";int *j;//indirectj=&i;cout<<j<<"\n";//shows the addressof icout<<*j<<"\n";//shows the value of i

cout<<(unsigned long)&j;//shows the address of jgetch();}

Page 227: BOOK on C++ Programs

// Program to demstrate the use of POINTER

#include<iostream.h>#include<conio.h>void main(){char var ='a';char *ptr=&var;cout<<var<<endl<<&var<<endl<<ptr<<endl<<*ptr<<endl<<++(*ptr)<<endl<<++(*(&var));cout<<endl<<*ptr++<<endl<<*ptr<<endl<<*(ptr++);getch();}

Page 228: BOOK on C++ Programs

// PROGRAM TO PRINT THE AREA OF SQUARE AND ITS PERIMETER

#include<iostream.h>void main (){int a,area,peri;cout<<"Enter a side";cin>>a;area=a*a;cout<<"Area of square"<<area<<endl;peri=4*a;cout<<"Perimeter of square"<<peri<<endl;}

Page 229: BOOK on C++ Programs

// Program for swaping two int number using pointer

#include<iostream.h>#include<conio.h>void main(){void swap(int*,int*);int a=10,b=20;swap(&a,&b);cout<<a<<endl<<b;}void swap(int *n ,int *m){

int t;t=*n;*n=*m;*m=t;getch();}

Page 230: BOOK on C++ Programs

// PROGRAM TO PRINT THE NUMBERS IN DESCENDING ORDER

#include<iostream.h>#include<conio.h>void main(){clrscr();int a[5],i,j,t;for(i=0;i<5;i++){cout<<"ENTER THE VALUE";cin>>a[i];}for(i=0;i<4;i++){for(j=i+1;j<5;j++){if(a[i]<a[j]){t=a[i];a[i]=a[j];a[j]=t;}}}for(i=0;i<5;i++)cout<<a[i]<<",";}

Page 231: BOOK on C++ Programs

// Program to show the use of satanderd input statement

#include<iostream.h>#include<conio.h>void main(){clrscr();int age;cout<<"please enter your present age";cin>>age;cout<<"your present age is"<<age;getch();}

Page 232: BOOK on C++ Programs

// program to check the number is prime or not

#include<iostream.h>#include<conio.h>void main(){clrscr();int i=2,num,c=0;cout<<"PLEASE ENTER THE BACAR NUMBER";cin>>num;while(i<num-1){if(num%i==0)c++;i++;}if(c==0)cout<<"the number is prime";elsecout<<"THE NUMBER not prime";getch();}

Page 233: BOOK on C++ Programs

// PROGRAM TO PRINT THE GIVEN DESIGN********************

#include<iostream.h>void main(){int i,j;for(i=1;i<=4;i++){for(j=1;j<=5;j++){cout<<"*";}cout<<"\n";}}

Page 234: BOOK on C++ Programs

// PROGRAM TO PRINT THE GIVEN STAR DESIGN

*************************

#include<iostream.h>void main(){int i,j;for(i=1;i<=5;i++){for(j=1;j<=i;j++){cout<<"*";}cout<<"\n";}for(i=4;i>=1;i--){for(j=1;j<=i;j++){cout<<"*";}cout<<"\n";}}

Page 235: BOOK on C++ Programs

// PROGRAM TO PRINT THE SQUARE OF THE FIRST 20 NUMBERS

#include<iostream.h>void main(){int c,s;c=0;k:c=c+1;s=c*c;cout<<s<<"\n";if(c<20) goto k;}

Page 236: BOOK on C++ Programs

// program enter any number from keyboard to reverse order

#include<iostream.h>#include<conio.h>void main(){clrscr();int num,sum=0,n;cout<<"please enter the integer number";cin>>num;while(num>0){n=num%10;cout<<n;num=num/10;}getch();}

Page 237: BOOK on C++ Programs

//revers function

#include<iostream.h>#include<conio.h>#include<string.h>main(){clrscr();char str1[10],str2[10];cout<<"enter the value";cin>>str1;strrev(str1);cout<<str1;getch();}

Page 238: BOOK on C++ Programs

// PROGRAM TO PRINT THE TABLE OF 5

#include<iostream.h> void main() { int i,p; for(i=1;i<=10;i++) { p=5*i; cout<<" "<<p<<"\n";

} }

Page 239: BOOK on C++ Programs

// Program to find the sum of row and colum in a Matrix#include<iostream.h>#include<conio.h>void main(){clrscr();int r,c,sum=0;for(r=1;r<=3;r++){for(c=1;c<=2;c++){cin>>are>>c;sum=r+c;}}cout<<sum;cout<<endl;getch();}

Page 240: BOOK on C++ Programs

// PROGRAM TO PRINT THE GIVEN DESIGN*******************************************************

#include<conio.h># include <iostream.h>void main(){clrscr();int i,j;for(i=1;i<=10;i++){for(j=1;j<=i;j++){cout<<"*";}cout<<"\n";}}

Page 241: BOOK on C++ Programs

// Program enter any integ no from keyboard and find the number is storng or not

#include<iostream.h>#include<conio.h>main(){clrscr();long num,rem,fact=1,sum=0,z ,n;cout<<"ENTER ANY DABBA NUMBER";cin>>num;z=num;while(num>0){n=num%10;fact=1;while(n>0){fact=fact*n;n--;}sum=sum+fact;num=num/10;}if(z==sum)cout<<"THE NUMBER IS AS STRONG AS ME";elsecout<<"THE NUMBER IS NOT STRONG LIKE YOU";

getch();

}

Page 242: BOOK on C++ Programs

//Program TO CALCULATE AVG MARKS

# include <iostream.h># include <conio.h>void main ()

{ int x[5],total,i; float avg; for(i=0;i<5;i++) { cout <<"\n enter marks scored by student"<<i; cin >>x[i]; total =total +x[i]; } avg=total/5; cout<<"average marks "<<avg; }

Page 243: BOOK on C++ Programs

// This program will print country,capital name,income depending on input

#include<iostream.h>#include<conio.h>#include<stdlib.h>#include<stdio.h>struct result{int a[5];char country[6];char capital[10];int income;}main(){int num;result report[5];for(num=0;num<5;num++){cout<<"enter the number";cin>>report[num].country;cout<<"enter the capital name";cin>>report[num].capital;cout<<"enter the income";cin>>report[num].income;}}

Page 244: BOOK on C++ Programs

// program is to input no. of weeks days from 1to7

#include<iostream.h>#include<conio.h>int main (){ clrscr(); int dow; cout << "enter no of week days(1to7)"; cin >>dow; switch (dow) { case 1:cout <<" \n sunday ";

break;

case 2: cout <<" \n monday "; break;

case 3: cout <<" \n tuesday "; break;

case 4: cout <<" \n wednessday "; break;

case 5: cout <<" \n thursday "; break;

case 6: cout <<" \n friday "; break;

case 7: { cout<<" \n saturday ";

break; } } return 0; }

Page 245: BOOK on C++ Programs

// Program to print the absolute value

#include<iostream.h>#include<conio.h>#include<math.h>void main(){clrscr();double a;double b;cout<<"enter the num"<<endl;cin>>a;b=fabs(a);cout<<b<<"\n";cout<< (int) ( b+.5);getch();}#include<iostream.h>#include<conio.h>void main(){clrscr();float bs,gs,hra,da;cout<<"please enter the bs";cin>>bs;if(bs<1500){hra=bs*10/100;da=bs*90/100;}else{hra=500;da=bs*98/100;}gs=bs+hra+da;cout<<"the gross salary is "<<gs;getch();}

Page 246: BOOK on C++ Programs

// Program to calculate basic salary

#include<iostream.h>#include<conio.h>void main(){clrscr();float b_s,da,hra,gross;cout<<" enter the B_s" ;cin>> b_s ;da=(b_s*40)/100;hra=(b_s*10)/100;gross=da+hra+b_s;cout<<"the gross salary is "<<gross;getch();}

Page 247: BOOK on C++ Programs

// Program to count the number of empoly earning more than 1lack. perannum.the monthly salary of 10 employ are input through key board

#include<iostream.h>#include<conio.h>main(){clrscr();long sal[10];long i=0,c=0,anual ;cout<<"enter the 10 empoly salary per month";for( i=0;i<10;i++)cin>>sal[i];{anual=sal[i]*12;}if(anual>100000)c++;cout<<c ;getch();}

Page 248: BOOK on C++ Programs

// Program to show the use of setfii()command

#include<iostream.h>#include<conio.h>#include<iomanip.h>void main(){clrscr();cout<<"welcome"<<setw(5)<<setfill('?')<<"as";getch();}

Page 249: BOOK on C++ Programs

// Program to show the use of setprecision()

#include<iostream.h>#include<conio.h>#include<iomanip.h>void main(){clrscr();float div;div=8/3;cout<<setprecision(5)<<div;getch();}

Page 250: BOOK on C++ Programs

// PROGRAM TO PRINT THE GIVEN SERIES// 1222333344444555555

#include<iostream.h>void main(){int i,j;for(i=1;i<=5;i++){for(j=1;j<=i;j++){cout<<i;}cout<<j;}}

Page 251: BOOK on C++ Programs

// PROGRAM TO PRINT THE GIVEN DESIGN ON SCREEN***************

#include<iostream.h>#include<conio.h>void main(){int i,j;clrscr();for(i=1;i<=5;i++){for(j=1;j<=i;j++){cout<<"*";}cout<<"\n";}}

Page 252: BOOK on C++ Programs

// Program to show the use of POINTER

# include<iostream.h># include<conio.h>void main(){clrscr ();int x;int * pointer;x =10;pointer=&x;cout<<"mm add="<<pointer;pointer++;cout<<"mm add=" <<pointer;getch();}

Page 253: BOOK on C++ Programs

//PROGRAM TO ENTER YOUR NAME AND PRINT IT 10 TIMES

#include<iostream.h>void main(){char n[50];int i;cout<<"Enter Name";cin>>n;for(i=0;i<10;i++){cout<<" "<<n<<endl;}}

Page 254: BOOK on C++ Programs

// PROGRAM TO PRINT A LINE 20 TIMES ON SCREEN

#include<iostream.h># include <conio.h>void main(){ clrscr ;int i=1;for(i=1;i<=20;i++){ cout<<"\n MANI CHANDRA IS A GOOD GIRL";}}

Page 255: BOOK on C++ Programs

//PROGRAM TO PRINT THE TABLE OF 8 & 9

# include<iostream.h># include<conio.h>void main (){clrscr();int m=0,n=99,i;for(i=1;i<=10;i++){m=m+8;n=n-9;cout<<"\n"<<m<<" "<<n;}}

Page 256: BOOK on C++ Programs

// PROGRAM TO PRINT A GIVEN DESIGN

544333222211111

# include<iostream.h>void main(){int i,j;for(i=5;i>=1;i--){for(j=5;j>=i;j--){cout<<i;}cout<<"\n";}}

Page 257: BOOK on C++ Programs

// Program to calculate the SP & CP

#include<iostream.h>#include<conio.h>void main(){clrscr();float cp, sp,profit,loss;cout<<"enter the black money cp";cin>>cp;cout<<" the the wight money sp";cin>>sp;profit=sp-cp;loss=cp-sp;if(sp>cp){ cout<<"please print unwanted profit"<<profit;} else{ cout<<"oh oh oh loss"<<loss;} getch(); }

Page 258: BOOK on C++ Programs

// Program to show the use of sqrt()

#include<iostream.h>#include<conio.h>#include<math.h>#include<iomanip.h>void main(){clrscr();cout<<setprecision(4)<<sqrt(7);getch();}

Page 259: BOOK on C++ Programs

// Program to print the greatest num in the row

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j,a[3][3];for(i=0;i<3;i++){for(j=0;j<3;j++){cout<<"enter the num of "<<endl;cin>>a[i][j];}}for(i=0;i<3;i++){int g=0;for(j=0;j<3;j++){cout<<a[i][j];if(a[i][j]>g){g=a[i][j];}}cout<<"the greatest num ="<<g<<endl;cout<<endl;}getch();

}

Page 260: BOOK on C++ Programs

// Program to copy one string two another string using pointer

#include<iostream.h>#include<conio.h>main(){clrscr();char str1[]="abcd",str2[20];char *p1,*p2;p1=str1;p2=str2;while(*p1!='\0'){*p2=*p1;p1++;p2++;}cout<<str2;

getch();}

Page 261: BOOK on C++ Programs

// Program to find whether a given no. is even or odd

#include<conio.h>#include<stdio.h>void main(){int a;clrscr();printf("enter the value of a");scanf("%d",&a);if (a%2==0)

{printf("even");}

else{printf("odd");}

getch();}

Page 262: BOOK on C++ Programs

// Program for sum of the digit int number using do while

#include<iostream.h>#include<conio.h>main(){clrscr();int num,sum=0,n;cout<<"enter the number";cin>>num;do{n=num%10;sum=sum+n;num=num/10;}while(num>0);cout<<sum;getch();}

Page 263: BOOK on C++ Programs

// Program to print ther sum of first 10 natural number.

#include<iostream.h>#include<conio.h>void main(){clrscr();int n=1,count=0;while(n<=10){count+=n;n=n+1;}cout<<count;getch();}

Page 264: BOOK on C++ Programs

// Program to calculate the SUM OF MATRIX 3*3 OF ABOVe DIGonal BELOW DIGonal

#include<iostream.h>#include<conio.h>main(){clrscr();int a[3][3],sum=0,sa=0,su=0,sd=0,i, j;cout<<"PLEASE ENTER THE ELEMENT$$$$$$$$$$$$";for( i=0;i<3;i++){for( j=0;j<3;j++)cin>>a[i][j];}for(i=0;i<3;i++){for(j=0;j<3;j++){if(i<j)sa=sa+a[i][j];elseif(j<i)su=su+a[i][j];elsesd=sd+a[i][j];}}cout<<"sum of above digonal"<<" "<< sa<<endl;cout<<"sum of under digonal"<<" "<< su<<endl;cout<<"sum of digonal"<< " "<< sd;

getch();}

Page 265: BOOK on C++ Programs

// Program to enter the value in an array and calculate the sum of element

#include<iostream.h>#include<conio.h>main(){ clrscr();int a[3][3],sum=0,num;cout<<"enter the valu of element";for(int i=0;i<3;i++){for(int j=0;j<3;j++){cin>>a[i][j];sum=sum+a[i][j];}}cout<<"the sum of element"<<sum;getch();}

Page 266: BOOK on C++ Programs

// Program to show the use of programming in Super Market

#include<iostream.h>#include<conio.h>#include<string.h>#include<iomanip.h>#include<graphics.h>#include<dos.h>void pu(){int gdriver=DETECT,gmode;initgraph(&gdriver,&gmode, "");setbkcolor(6);}int k=0;class base

{int i,total;int sno,qno[10],qty[10],up[10],amt[10];char qname[10][20],ch;public:

base(){sno=1;}

void get();void line();void show();void display();

};void base::get()

{total = 0;ch='y';pu();void p();p();while(ch=='y' || ch=='Y' )

{cout<<"\nEnter quantity number: ";cin>>qno[sno];if(qno[sno]==1)strcpy(qname[sno],"Provision");else if(qno[sno]==2)strcpy(qname[sno],"Grossary");else if(qno[sno]==3)strcpy(qname[sno],"Crocery");else if(qno[sno]==4)strcpy(qname[sno],"Eatables");elsestrcpy(qname[sno],"Miscellaneous");cout<<"Enter the quantity : ";cin>>qty[sno];cout<<"Enter the unit price: ";cin>>up[sno];amt[sno]=up[sno]*qty[sno];

Page 267: BOOK on C++ Programs

total=total+amt[sno];sno++;cout<<"Do you want to continue(Y/N) ";ch=getch();

}}

void p(){pu();cout<<"É";

for(int i=1;i<78;i++)cout<<"Í";cout<<"»\n";cout<<"º P U N E E T PROVISIONAL STORES PVT. LTD.

º\n";cout<<"º PGDCA , SESSION:2001-2002, TMIMT Delhi

Road, Moradabad º\n";cout<<"º Phone no.: 318401,Fax no. :484244,Mobile

no.:983713231 º\n";cout<<"È";for(i=1;i<78;i++)cout<<"Í";cout<<"¼\n";cout<<"\n\n";

}void base::show()

{void p();p();cout<<"\t\t\t\t B I L L\n";for(i=1;i<78;i++)cout<<"Í";cout.width(1);cout.setf(ios::internal);cout<<"\nSno";cout.width(20);cout.setf(ios::right);cout<<"Product name";cout.width(15);cout.setf(ios::internal);cout<<"Quanity";cout.width(15);cout.setf(ios::internal);cout<<"Unit price";cout.width(20);cout.setf(ios::internal);cout<<"Amount";cout<<"\n";for(i=1;i<78;i++)cout<<"Í";

}void base::line(){

}void base::display()

Page 268: BOOK on C++ Programs

{for(int i=1;i<sno;i++)

{cout<<endl;cout.width(2);cout.setf(ios::internal);cout<<i;cout.width(20);cout.setf(ios::internal);cout<<qname[i];cout.width(12);cout.setf(ios::internal);cout<<qty[i];cout.width(12);cout.setf(ios::internal);cout<<up[i];cout.width(24);cout.setf(ios::internal);cout<<amt[i];

}cout<<"\n";for(i=1;i<78;i++)cout<<"Í";cout<<"\n";cout.width(2);cout.setf(ios::internal);cout<<"TOTAL Amount in Rupees:";cout.width(47);cout.setf(ios::internal);cout<<total;cout<<"\n";for(i=1;i<78;i++)cout<<"Í";cout<<"\n\n";cout<<"Thank you for your Kind Visit, Please Visit us again ";}void main(){

clrscr();pu();

int i=30;gotoxy(i,8);delay(400);cout<<"W";gotoxy(i+=2,8);delay(400);cout<<"E";gotoxy(i+=2,8);delay(400);cout<<"L";gotoxy(i+=2,8);delay(400);cout<<"C";gotoxy(i+=2,8);delay(400);cout<<"O";gotoxy(i+=2,8);

Page 269: BOOK on C++ Programs

delay(400);cout<<"M";gotoxy(i+=2,8);delay(400);cout<<"E";i=33;gotoxy(i+=2,9);delay(400);cout<<"T";gotoxy(i+=2,9);delay(400);cout<<"O";i=32 ;gotoxy(i+=2,10);delay(400);cout<<"T";gotoxy(i+=2,10);delay(400);cout<<"H";gotoxy(i+=2,10);delay(400);cout<<"E";

i=28;gotoxy(i+=2,11);delay(400);cout<<"P";gotoxy(i+=2,11);delay(400);cout<<"R";gotoxy(i+=2,11);delay(400);cout<<"O";gotoxy(i+=2,11);delay(400);cout<<"J";gotoxy(i+=2,11);delay(400);cout<<"E";gotoxy(i+=2,11);delay(400);cout<<"C";gotoxy(i+=2,11);delay(400);cout<<"T";i=33;gotoxy(i+=2,12);delay(400);cout<<"O";gotoxy(i+=2,12);delay(400);cout<<"N";gotoxy(i+=2,12);delay(400);i=27;gotoxy(i+=2,13);delay(400);

Page 270: BOOK on C++ Programs

cout<<"S";gotoxy(i+=2,13);delay(400);cout<<"U";gotoxy(i+=2,13);delay(400);cout<<"P";gotoxy(i+=2,13);delay(400);cout<<"E";gotoxy(i+=2,13);delay(400);cout<<"R";gotoxy(i+=2,13);delay(400);gotoxy(i+=2,13);delay(400);cout<<"M";gotoxy(i+=2,13);delay(400);cout<<"A";i= 30 ;gotoxy(i+=2,14);delay(400);cout<<"R";gotoxy(i+=2,14);delay(400);cout<<"K";gotoxy(i+=2,14);delay(400);cout<<"E";gotoxy(i+=2,14);delay(400);cout<<"T";gotoxy(i+=2,14);delay(400);cout<<"E";(25,18);delay(400);cout<<"~~~~~~~~~~~~~~~~~~~~~~~";delay(700);gotoxy(60,23);cout<<"By:Arvind Gaba ";getch( );

pu();clrscr();base b;b.get();clrscr();b.show();b.display();b.line();getch();

}

Page 271: BOOK on C++ Programs

// Program to print the name of the week

#include<iostream.h>#include<conio.h>void main(){clrscr();int x;cout<<"enter any no 1 to 7";cin>>x;switch(x){case 1:cout<<"the day is sunday";break;case 2:cout<<"the day is monday";break;case 3:cout<<"the day is tuesday";break;case 4:cout<<"the day is wednesday";break;case 5:cout<<"the day is thursday";break;case 6:cout<<"the day is friday";break;case 7:cout<<"the day is saturday";break;default:cout<<"involid choice";}getch();}

Page 272: BOOK on C++ Programs

// Program to print the table of any number

#include<iostream.h>#include<conio.h>void main(){clrscr();int n, count=1;cout<<"please enter the number which table you want";cin>>n;while(count<=10){cout<<n<<"*"<<count<<"="<<count*n<<"\n";count=count+1;}getch();}

Page 273: BOOK on C++ Programs

// program to print the table of any number

#include<iostream.h>#include<conio.h>void main(){clrscr();int count=5;while(count<=50){cout<<count<<"\n";count=count+5;}getch();}

Page 274: BOOK on C++ Programs

// program to find simple interest

#include<conio.h>#include<stdio.h>void main(){int p,r,t,si;clrscr();printf("enter the values of p,r,t");scanf("%d %d %d",&p,&r,&t);si=(p*r*t)/100;printf("\ninterest=%d",si);getch();}

Page 275: BOOK on C++ Programs

// program to find the area and circumference of a circle

#include<conio.h>#include<stdio.h>#include<math.h>void main(){float a,c,r,pi;clrscr();printf("enter the value of r");scanf("%f",&r);pi=3.14;a=pi*pow(r,2);printf("area=%f",a);c=2*pi*r;printf("\ncircumference=%f",c);getch();}

Page 276: BOOK on C++ Programs

// program to find the volume and surface area of a sphere

#include<conio.h>#include<stdio.h>#include<math.h>void main(){float r,v,s,pi;clrscr();printf("enter the value of r");scanf("%f",&r);pi=3.14;v=(4*pi*pow(r,3))/3;printf("volume=%f",v);s=4*pi*pow(r,2);printf("\narea=%f",s);getch();}

Page 277: BOOK on C++ Programs

// program to print the largest no. of the three given nos.

#include<conio.h>#include<stdio.h>void main(){float a,b,c;clrscr();printf("enter the values of a,b,c");scanf("%f %f %f",&a,&b,&c);if(a>b&&a>c)

{printf("largest no.=%f",a);}

if(b>a&&b>c){printf("largest no.=%f",b);}

if(c>a&&c>b){printf("largest no.=%f",c);}

getch();}

Page 278: BOOK on C++ Programs

// program to find the discount rate

#include<conio.h>#include<stdio.h>void main(){double r,d,s,t;clrscr();d=0;printf("enter the value olf r");scanf("%lf",&r);if((r>=2000)&&(r<5000))d=15;if(r>=5000)d=20;s=(r*d)/100;t=r-s;printf("%lf",t);getch();}

Page 279: BOOK on C++ Programs

// program to arrange a list of given nos. in ascending order using array

#include<conio.h>#include<stdio.h>void main(){int list[100],i,j,c,n;clrscr();printf("enter size");scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&list[i]);for(i=0;i<n;i++){

for(j=i+1;j<n;j++){

if(list[i]>list[j]){ c=list[i];

list[i]=list[j];list[j]=c;

}}

}printf("array is started from");for(i=0;i<n;i++)printf("%d ",list[i]);getch();}

Page 280: BOOK on C++ Programs

// program to check whether a given no. is +ve,-ve or zero"

#include<process.h>#include<conio.h>#include<stdio.h>void main(){int a;clrscr();printf("enter the value of a");scanf("%d",&a);if (a<0){printf("negative");}if (a>0){printf("positive");}if (a==0){printf("zero");}getch();}

Page 281: BOOK on C++ Programs

// program to check whether a given no. is prime or not

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

void main(){int i,x,c;clrscr();c=0;scanf("%d",&x);for(i=2;i<=x/2;i++){if(x%i==0){c=1;break;

}elsec=0;}if (c==1)printf("not prime");elseprintf("prime");getch();}

Page 282: BOOK on C++ Programs

// program to find the diff. and div. of two real nos.

#include<conio.h>#include<stdio.h>void main(){float a,b,s,t;clrscr();printf("enter the values of a,b");scanf("%f %f",&a,&b);s=b-a;printf("diff.=%f",s);t=b/a;printf("\ndiv.=%f",t);getch();}

Page 283: BOOK on C++ Programs

// Program to calculate the area and perimeter of a rectangle

#include<conio.h>#include<stdio.h>void main(){int a,b,ar,p;clrscr();printf("enter the values of a,b");scanf("%d %d",&a,&b);ar=a*b;printf("area=%d",ar);p=2*(a+b);printf("\nperimeter=%d",p);getch();}

Page 284: BOOK on C++ Programs

// program to exchange two nos.#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){int x,y,z;clrscr();printf("enter the values of x,y");scanf("%d %d",&x,&y);z=x;x=y;y=z;printf("\n the value of x=%d",x);printf("\n the value of y=%d",y);getch();}

Page 285: BOOK on C++ Programs

// PROGRAM TO PRINT UR NAME N SCREEN

#include<iostream.h>void main(){cout<<"\n sumbul";}

Page 286: BOOK on C++ Programs

// program to print given design

544333222211111

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j,x=5;for(i=1;i<=5;i++){for(j=1;j<=i;j++){cout<<x;}cout<<endl;x=x-1;}}

Page 287: BOOK on C++ Programs

//Program to find the sum of two arrays.

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){int a[100],b[100],i,c[100],n;clrscr();printf("enter the size of array1");scanf("%d",&n);printf("enter the size of array2");scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<n;i++)scanf("%d",&b[i]);for(i=0;i<n;i++)c[i]=a[i]+b[i];for(i=0;i<n;i++)printf(" %d ",c[i]);getch();}

Page 288: BOOK on C++ Programs

// program to write the eqn. of line

#include<conio.h>#include<stdio.h>void main(){int x1,x2,y1,y2,m,c;clrscr();printf("enter the values of x1,y1");scanf("%d %d",&x1,&y1);printf("enter the values of x2,y2");scanf("%d %d",&x2,&y2);m=(y2-y1)/(x2-x1);c=y1-m*x1;printf("y=%dx+%d",m,c);getch();}

Page 289: BOOK on C++ Programs

// program to find the area of a triangle

#include<conio.h>#include<stdio.h>#include<math.h>

void main(){float a,b,c,s,ar;clrscr();printf("enter the values of a,b,c");scanf("%f %f %f",&a,&b,&c);s=(a+b+c)/2;ar=sqrt(s*(s-a)*(s-b)*(s-c));printf("area of triangle=%f",ar);getch();}

Page 290: BOOK on C++ Programs

// program to find the roots of eqn.

#include<conio.h>#include<stdio.h>#include<math.h>void main(){double a,b,c,r,s,t;clrscr();printf("enter the values of a,b,c");scanf("%lf %lf %lf",&a,&b,&c);s=sqrt(pow(b,2)-(4*a*c));r=(-b+s)/(2*a);printf("root1=%lf",r);t=(-b-s)/(2*a);printf("\nroot2=%f",t);getch();}

Page 291: BOOK on C++ Programs

// program to calculate the velocity

#include<conio.h>#include<stdio.h>void main(){int v,u,a,t;clrscr();printf("enter the values of u,a,t");scanf("%d %d %d",&u,&a,&t);v=u+(a*t);printf("velocity=%d",v);getch();}

Page 292: BOOK on C++ Programs

// program to calculate the mass energy equivalence

#include<conio.h>#include<stdio.h>#include<math.h>void main(){double e,m,c;clrscr();printf("enter the value of m");scanf("%lf",&m);c=3e8;e=m*pow(c,2);printf("e=%lf",e);getch();}

Page 293: BOOK on C++ Programs

// program to change farenheit temp. to degree centigrade

#include<conio.h>#include<stdio.h>void main(){float c,f;clrscr();printf("enter the value of f");scanf("%f",&f);c=((f-32)*5)/9;printf("temp. in celcius=%f",c);getch();}

Page 294: BOOK on C++ Programs

//PROGRAM TO PRINT THE TABLE OF A GIVEN NUMBER BY THE USER

# include <iostream.h>#include <conio.h>int main(){clrscr();int num;cout<<"\n enter number:";cin>>num;for(int i=1;i<11;++i)cout <<"\n"<<num<<"*"<<i<<"="<<num*i;return 0;}

Page 295: BOOK on C++ Programs

// program to print sum of the digits

#include<iostream.h>void main()

{int n,s=0,a;cout<<"enter the number";cin>>n;do{a=n%10;s=s+a;n=n/10;}while(n!=0)cout<<s;}

Page 296: BOOK on C++ Programs

//PROGRAM TO PRINT A TABLE OF 20

#include <iostream.h>void main(){int i,a=20,p;

for(i=1;i<=10;i++){p=a*i;cout<<a<<"*"<<i<<"="<<p<<"\n";}}

Page 297: BOOK on C++ Programs

// to print the given the given digit

#include <iostream.h>void main(){int i,j,k,n=40;i=-1;do{i=i+2;j=0;do{j=j+1;cout<<" ";}while(j<n);n=n-1;k=0;do{k=k+1;cout<<"*";}while(k<i);cout<<"\n";}while(i<9);}

Page 298: BOOK on C++ Programs

// program to transpose of matrix

#include<iostream.h>#include<conio.h>main(){clrscr();int a[3][3];cout<<"PLEASE ENTER THE ELEMENT OF MATRIX===========";for(int i=0;i<3;i++){for(int j=0; j<3;j++){cin>>a[i][j];}}for(i=0;i<3;i++){for(int j=0;j<3;j++){cout<<endl<< a[j][i];}}getch();}

Page 299: BOOK on C++ Programs

// program which will ask you to enter two integer from the keyboard

and display its sum#include<iostream.h>#include<conio.h>void main(){clrscr();int a,b,c;cout<<"please enter the value a";cin>>a;cout<<"please enter the value b";cin>>b;c=a+b;cout<<"the total value is"<<c;getch();}

Page 300: BOOK on C++ Programs

// program to show the use of pointer of data type void

#include<iostream.h> #include<conio.h> main() { clrscr(); char var='a'; char *ptr=&var; void *char_ptr=&var; cout<<*(char*)char_ptr<<endl; cout<<(int&)var<<endl; cout<<(int&)char_ptr<<endl; getch(); }

//Program to

#include<iostream.h> #include<conio.h> main() { clrscr(); int r,c; for(r=1;r<=25;r++)

{ cout<<endl;

for(c=25;c>=r;c--) { cout<<"$"<<" "; } } getch(); }

Page 301: BOOK on C++ Programs

// program to print given design

#include<iostream.h>#include<conio.h>void main(){clrscr();int c=0,l,k,j,n=40;do{c=c+1;k=0;do{k=k+1;cout<<" ";}while(k<n);j=0;do{j=j+1;cout<<j;}while(j<c);l=c;do{l=l-1;if(l==0)goto zz;cout<<l;zz:}while(l>1);n=n-1;cout<<endl;}while(c<9);}//end main//

Page 302: BOOK on C++ Programs

// program to display a matrix

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j;int a[3][3];for(i=0;i<=3;i++){for(j=0;j<3;j++){cout<<"enter the num"<<endl;cin>> a[i][j];}}for(i=0;i<3;i++){for(j=0;j<3;j++){cout<<a[i][j];}cout<<endl;}getch();}

Page 303: BOOK on C++ Programs

// program to print the memory location before and after the increment

#include<iostream.h>#include<conio.h>void main(){clrscr();int x;int*pointer;x=10;pointer=&x;cout<<"Memory location before incrementation =";cout<<pointer;pointer++;cout<<endl;cout<<"Memory location after incrementation =";cout<<pointer;getch();}

Page 304: BOOK on C++ Programs

// program is to print given design

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j,x=5;for(i=1;i<=5;i++){for(j=1;j<=i;j++){cout<<x;}cout<<endl;x=x-1;}}

Page 305: BOOK on C++ Programs

// Program to find whether the no. is odd or even

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){int a,k;clrscr();printf("enter");scanf("%d",&a);k=1;case1:if(a%2==0)case2:if(a%2!=0)switch(k){

case 1:printf("even");break;case 2:printf("odd");break;

}getch();}

Page 306: BOOK on C++ Programs

// program to print the squire of int from 1 to 10

#include<iostream.h>#include<conio.h>void main(){clrscr();int i;for(i=1;i<=10;i++){ cout<<endl;cout<<i*i;}getch();}

Page 307: BOOK on C++ Programs

// program to print given design

123451234123121

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j;for(i=5;i>=1;i--){for(j=1;j<=i;j++){cout<<j;}cout<<endl;}}

Page 308: BOOK on C++ Programs

// PROGRAM TO PRINT THE GIVEN DESIGN

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

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

void main(){ clrscr(); char i; for(i=65;i<=90;i++) cout<<i<<" ";}

Page 309: BOOK on C++ Programs

// PROGRAM TO PRIT THE SQUARE OF GIVEN NUMBERS

#include<iostream.h>#include<conio.h>void main(){ clrscr(); int c=0,i; k:c=c+1; i=c*c; cout<<i<<"\n"; if (c<10) goto k; j:c=c-1; i=c*c; cout<<i<<"\n"; if(c>1) goto j;}

Page 310: BOOK on C++ Programs

// program to check whether the character entered is a vowel or not

#include<iostream.h>#include<conio.h>void main(){clrscr();char ch;cout<<"enter any c haracter";cin>>ch;switch(ch){case'a':case'A':case'e':case'E':case'i':case'I':case'o':case'O':case'u':case'U':cout<<"it is a vowel:"<<ch;break;default:cout<<"it is a consonent";}getch();}

Page 311: BOOK on C++ Programs

// program is to print given design

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j;for(i=5;i>=1;i--){for(j=1;j<=i;j++){cout<<j;}cout<<endl;}}

Page 312: BOOK on C++ Programs

// program to calculate the area and perimeter of a rectangle

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){int a,b,ar,p;clrscr();cout<<"enter the values of a,b";cin>>a>>b;ar=a*b;cout<<"area="<<ar;p=2*(a+b);cout<<"\n perimeter="<<p;getch();}

Page 313: BOOK on C++ Programs

// program to find area of a triangle

#include<iostream.h>#include<conio.h>#include<stdio.h>#include<math.h>void main(){clrscr();float a,b,c,s,g;cout<<"enter the values of a b c";cin>>a>>b>>c;s=(a+b+c)/2;float f;f=s*(s-a)*(s-b)*(s-c);g=pow(f,1/2);cout<<"the area of triangle ="<<g;getch();}

Page 314: BOOK on C++ Programs

//PROGRAM TO PRINT A WELCOME MESSAGE

#include<iostream.h>#include<conio.h>void main(){clrscr();cout<<"\n Welcome to C++ programming";}

Page 315: BOOK on C++ Programs

// Program to convert distance into meters by using operator overloading.

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

const float MTF=3.28;

class distance{

int feet;float inches;public:

distance(){

feet=0;inches=0;

}

distance(float metres){

float fltfeet=MTF*metres;feet=int(fltfeet);inches=12*(fltfeet-feet);

}

distance(int ft,float in){

feet=ft;inches=in;

}

void getlist(){

cout<<"\nEnter Feet: ";cin>>feet;cout<<"\nEnter Inches: ";cin>>inches;

}

void showlist(){

cout<<"\nFeet = "<<feet;cout<<"\nInches = "<<inches;

}

operator float(){

float fracfeet= inches/12;fracfeet+=float(feet);return(fracfeet/MTF);

}

Page 316: BOOK on C++ Programs

};

void main(){

clrscr();distance dist1=2.34;cout<<"\nDist1 = ";dist1.showlist();cout<<"\nDist1 = ";dist1.showlist();distance dist2 = (5,10.25);float mtrs=float(dist2);cout<<"\nDist2 = "<<mtrs <<"metres";mtrs=dist1;cout<<"\nDist1 = "<<mtrs <<"metres";getch();

}

Page 317: BOOK on C++ Programs

// Program to multiply two matrices by passing them to a function

#include<iostream.h>#include<conio.h>#define MAX 100

void input(int a[][MAX],int b[][MAX],int n);void mult(int a[][MAX],int b[][MAX],int n);void display(int m[][MAX],int n);

void main(){

int a[MAX][MAX],b[MAX][MAX],m[MAX][MAX],n;clrscr();cout<<"Enter Size:";cin>>n;input(a,b,n);getch();

}

void input(int a[][MAX],int b[][MAX],int n){

cout<<"\nEnter Elements For Matrix A\n";for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cin>>a[i][j];}

}cout<<"\nEnter Elements For Matrix B\n";for(i=0;i<n;i++){

for(int j=0;j<n;j++){

cin>>b[i][j];}

}mult(a,b,n);

}

void mult(int a[][MAX],int b[][MAX],int n){

int m[MAX][MAX];for(int i=0;i<n;i++){

for(int j=0;j<n;j++){ m[i][j]=0;

for(int k=0;k<n;k++){

m[i][j]=m[i][j]+(a[i][k]+b[k][j]); }}

Page 318: BOOK on C++ Programs

}display(m,n);

}

void display(int m[][MAX],int n){

cout<<"Multiplication Of Matrixes Is\n";for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cout<<"\t"<< m[i][j];}cout<<"\n";

}}

Page 319: BOOK on C++ Programs

// Program to overload '++' operator in a class in c++

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

class counter{

int count;public:

counter(){

count=0;}void getcount(){

cout<<"\nCount= "<<count;}void operator++(){

count++;}

};

void main(){

clrscr();counter c1,c2;c1.getcount();c2.getcount();c1++;c1.getcount();++c1;c1.getcount();getch();

}

Page 320: BOOK on C++ Programs

// Program to show the addition of two structure type objects

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

struct point{

int x;int y;

};

void main(){

clrscr();point a,b,c;cout<<"\nEnter Values:\n";cin>>a.x>>a.y;cin>>b.x>>b.y;c.x=a.x+b.x;c.y=a.y+b.y;cout<<"\nSum Of Points Is:\n "<<c.x<<"\n"<<c.y;getch();

}

Page 321: BOOK on C++ Programs

// Program to subtract two matrices by passing them to a function

#include<iostream.h>#include<conio.h>#define MAX 100

void input(int a[][MAX],int b[][MAX],int n);void sub(int a[][MAX],int b[][MAX],int n);void display(int s[][MAX],int n);

void main(){

int a[MAX][MAX],b[MAX][MAX],s[MAX][MAX],n;clrscr();cout<<"Enter Size:";cin>>n;input(a,b,n);getch();

}

void input(int a[][MAX],int b[][MAX],int n){

cout<<"\nEnter Elements For Matrix A\n";for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cin>>a[i][j];}

}cout<<"\nEnter Elements For Matrix B\n";for(i=0;i<n;i++){

for(int j=0;j<n;j++){

cin>>b[i][j];}

}sub(a,b,n);

}

void sub(int a[][MAX],int b[][MAX],int n){

int s[MAX][MAX];for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

s[i][j]=a[i][j]-b[i][j];}

}display(s,n);

}

void display(int s[][MAX],int n)

Page 322: BOOK on C++ Programs

{cout<<"Subraction Of Matrixes Is\n";for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cout<<"\t"<< s[i][j];}cout<<"\n";

}}

Page 323: BOOK on C++ Programs

// Program to transpose a 2D array by passing it to a function.

#include<iostream.h>#include<conio.h>#define MAX 100

void trans(int b[MAX][MAX],int n);void input(int a[MAX][MAX],int n);void display(int a[][MAX],int n);

void main(){

clrscr();int a[MAX][MAX],n;cout<<"Enter Size\n";cin>>n;input(a,n);getch();

}

void input(int a[][MAX],int n){

cout<<"\nEnter Elements For Matrix\n";for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cin>>a[i][j];}

}display(a,n);trans(a,n);

}

void trans(int a[][MAX],int n){

cout<<"\n Tranpose Of Matrix Is\n";for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cout<<a[j][i]<<"\t";}cout<<"\n";

}}

void display(int a[][MAX],int n){

cout<<"\n Matrix is\n";for(int i=0;i<n;i++){

Page 324: BOOK on C++ Programs

for(int j=0;j<n;j++){

cout<<a[i][j]<<"\t";}cout<<"\n";

}

}

Page 325: BOOK on C++ Programs

// Program to Print an array in reverse order

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

class a{

int a[10];public:

void input();void display();void reverse();

};

void a::input(){ cout<<"Enter Array Elements:\n "; for(int i=0;i<10;i++)

{ cin>>a[i];}

display();reverse();

}

void a::display(){

cout<<"The Array Elements Are\n";for(int i=0;i<10;i++)

cout<<a[i]<<" ";}

void a::reverse(){

int c[10],b=0;for(int i=9;i>=0;i--)

c[b++]=a[i];cout<<"\n The Reverse Of Array Is:\n";for(i=0;i<10;i++)

cout<<c[i]<<" ";}

void main(){

clrscr();a a1;a1.input();getch();

}

Page 326: BOOK on C++ Programs

// Program of conversion of integer into words.#include<iostream.h>#include<conio.h>void main(){

clrscr();char ones[9][10]={"one", "two", "three", "four", "five", "six",

"seven", "eight", "nine"};char tens[10][10]={"ten”,” eleven", "twelve", "thirteen",

"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

char etc[8][10]={"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

long int num;int a[10], i=0, temp, j;do{

clrscr();cout<<"\n Enter any number (<100) : ";cin>>num;

}while(num>=100);int s = num;do{

a[i++]=num%10;num = num/10;

}while(num!=0);i--;if(i==0){

cout<<ones[--s];}elseif(i==1&&a[i]==1){

temp = a[0];cout<<tens[temp];

}elseif(i==1){

temp = a[1];temp-=2;cout<<etc[temp];if(a[0]!=0){

temp = a[0];cout<<" "<<ones[--temp];

}}

}

Page 327: BOOK on C++ Programs

// Program to sum the digits of a number.

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

void main(){

clrscr();int num, s=0;cout<<"Enter any number : ";cin>>num;do{

s+=num%10;num/=10;

}while(num!=0);cout<<"\nSum = "<<s;getch();

}

Page 328: BOOK on C++ Programs

// Program of reverse of a number.

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

void main(){

clrscr();int num, s=0, temp;cout<<"Enter any number : ";cin>>num;do{

temp=(num%10);s = s*10+temp;num/=10;

}while(num!=0);cout<<"\nReverse = "<<s;getch();

}

Page 329: BOOK on C++ Programs

// Program to swap two numbers.

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

void main(){

clrscr();int num1, num2, temp;cout<<"Enter any two numbers : ";cin>>num1>>num2;num1+=num2;num2=num1-num2;num1=num1-num2;cout<<"\nFirst Number : "<<num1;cout<<"\nSecond Number : "<<num2;getch();

}

Page 330: BOOK on C++ Programs

// Program to calculate lower & upper triangle of a matrix.

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

void main(){

clrscr();int a[10][10], lt=0, rt=0, i, j, n;cout<<"Enter the size (<10) : ";cin>>n;cout<<"\nEnter the elements of matrix.\n";for(i=0; i<n; i++)

for(j=0; j<n; j++)cin>>a[i][j];

cout<<"\nEntered matrix.\n";for(i=0; i<n; i++){

for(j=0; j<n; j++)cout<<a[i][j]<<" ";

cout<<endl;}

for(i=0; i<n; i++)for(j=0; j<n; j++){

if(i>j)lt+=a[i][j];

if(j>i)rt+=a[i][j];

}cout<<"\nLeft : "<<lt;cout<<"\nRight : "<<rt;getch();

}

Page 331: BOOK on C++ Programs

// Program to add two distance in feets and inches by '+' operator.

#include<iostream.h>#include<conio.h>class distance{

int feet;float inch;public:

distance(){

feet=0;inch=0;

}void getdata(int f,float i){

feet=f;inch=i;

}void showdata(){

cout<<"\nFeets Are: "<<feet;cout<<"\nInches Are: "<<inch;

}distance operator+(distance d){

distance d1;d1.feet=feet+d.feet;d1.inch=inch+d.inch;while(inch>12){

d1.inch-=12;d1.feet;

}return d1;

} }; void main() {

clrscr();distance dist1,dist2,dist3;dist1.getdata(12,5.6);dist2.getdata(10,4.4);dist1.showdata();dist2.showdata();dist3=dist1+dist2;dist3.showdata();

}

Page 332: BOOK on C++ Programs

// Program to show a toll booth functioning by using classes.

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

class tollbooth{

unsigned int tot; double amt; public:

tollbooth(){

tot=0;amt=0;

}void payingcar(){

tot++;amt=amt+10;

}void nonpaying(){

tot++;}void display(){

cout<<"\n\n\t\tTotal Cars : "<<tot;cout<<"\n\t\tTotal Amount : "<<amt;

}};

void main(){

tollbooth t1;int ch;

while(1) {

clrscr();cout<<"\n Main Menu";cout<<"\n1.No. Of Paying Cars";cout<<"\n2.No. Of Non Paying Cars";cout<<"\n3.Display&Exit";cout<<"\nEnter Choice(1_3):";cin>>ch;switch(ch){

case 1:t1.payingcar();break;

case 2:t1.nonpaying();break;

case 3:t1.display();

Page 333: BOOK on C++ Programs

getch();exit(0);

default:cout<<"\n Wrong Choice";

} }}

Page 334: BOOK on C++ Programs

// Program to compare two values by enum datay type and operator overloading.

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

enum result{y,n};

class distance{

int feet;float inch;

public:distance(){

feet=0;inch=0;

}

distance(int f,float i){

feet=f;inch=i;

}

void getdata(){

cout<<"\nEnter The Values (Feet & Inches): ";cin>>feet>>inch;

}

void showdata(){

cout<<"\nFeets Are: "<<feet;cout<<"\nInches Are: "<<inch;

}

result operator< (distance d){

inch=inch+(feet*12);d.inch=d.inch+(d.feet*12);if(inch<d.inch)

return y;else

return n;}

};

void main() {

clrscr();distance dist1(10,8.5);distance dist2;

Page 335: BOOK on C++ Programs

dist2.getdata();dist1.showdata();dist2.showdata();if((dist1<dist2)==result(0))

cout<<"\nDist1 is less";else

cout<<"\nDist2 is less";getch();

}

Page 336: BOOK on C++ Programs

// Program to enter distance in feets and inches. Display it using classes.

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

class distance{

int feet;float inches;

public:void setdata(int f,float i){

feet=f;inches=i;

}

void getdata(){

cout<<"\nEnter Feet: ";cin>>feet;cout<<"\nEnter Inches: ";cin>>inches;

}

void display(){

cout<<"\nFeet Are: "<<feet;cout<<"\nInches Are: "<<inches;

}};

void main(){

clrscr();distance d1,d2;d1.setdata(12,5.6);d2.getdata();d1.display();d2.display();getch();

}

Page 337: BOOK on C++ Programs

// Program To Add two matrices by passing them to a function

#include<iostream.h>#include<conio.h>#define MAX 100

void input(int a[][MAX],int b[][MAX],int n);void add(int a[][MAX],int b[][MAX],int n);void display(int s[][MAX],int n);

void main(){

int a[MAX][MAX],b[MAX][MAX],s[MAX][MAX],n;clrscr();cout<<"Enter Size:";cin>>n;input(a,b,n);getch();

}

void input(int a[][MAX],int b[][MAX],int n){

cout<<"\nEnter Elements For Matrix A\n";for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cin>>a[i][j];}

}cout<<"\nEnter Elements For Matrix B\n";for(i=0;i<n;i++){

for(int j=0;j<n;j++){

cin>>b[i][j];}

}add(a,b,n);

}

void add(int a[][MAX],int b[][MAX],int n){

int s[MAX][MAX];for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

s[i][j]=a[i][j]+b[i][j];}

}display(s,n);

}

void display(int s[][MAX],int n)

Page 338: BOOK on C++ Programs

{cout<<"Addition Of Matrixes Is\n";for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cout<<"\t"<< s[i][j];}cout<<"\n";

}}

Page 339: BOOK on C++ Programs

// Program to overload '=' operator in a class in C++.

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

class counter{

int a;float b;public :

counter(int x, float y){

a = x;b = y;

}

void display(){

cout<<endl<<a<<endl<<b;}

void operator=(counter m){

a = m.a;b = m.b;

}}

void main(void){

counter c1(10,2.506);counter c2(9,5.908);clrscr();cout<<"\nInitial Values are : ";c1.display();c2.display();c2.operator=(c1);cout<<"\nFinal Values : ";c2.display();getch();

}

Page 340: BOOK on C++ Programs

// Program to show the use of array of objects of class type.

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

class distance{

int feet;float inch;

public:distance(){

feet=0;inch=0;

}

void getdata(){

cout<<"\nEnter feets & inches : ";cin>>feet>>inch;

}

void showdata(){

cout<<"\nFeets Are: "<<feet;cout<<"\nInches Are: "<<inch;

} };

void main() {

clrscr();distance dist[10];char ans;int n = 0;do{

cout<<"\nEnter the value : ";dist[n].getdata();dist[n].showdata();cout<<"\nWanna to continue........(y ro n) : ";cin>>ans;n++;

}while(ans=='y'||ans=='Y');getch();

}

Page 341: BOOK on C++ Programs

// Program to show the addition of two class objects in c++.

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

class distance{

int feet;float inch;

public:distance(){

feet=0;inch=0;

}void getdata(int f, float i){

feet = f;inch = i;

}void showdata(){

cout<<"\nFeets Are: "<<feet;cout<<"\nInches Are: "<<inch;

}void add_obj(distance d1, distance d2){

inch = d1.inch+d2.inch;while(inch>=12){

inch-=12;feet++;

}feet += d1.feet+d2.feet;

} };

void main() {

clrscr();distance dd1, dd2, dd3;dd1.getdata(10, 11.256);dd2.getdata(11, 10.956);dd1.showdata();dd2.showdata();dd3.add_obj(dd1,dd2);dd3.showdata();getch();

}

Page 342: BOOK on C++ Programs

// Program to create a class bank and insert data into it.

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

class bank{

char name[100];int acc;char type[100];int bal;

public:void getamount(){

cout<<"\nEnter the name of account holder : ";gets(name);cout<<"\nEnter the Account No. : ";cin>>acc;cout<<"\nEnter the Account Type : ";gets(type);cout<<"\nEnter the balance : ";cin>>bal;

}

void deposit(){

int d;cout<<"\nEnter the amount to be deposited : ";cin>>d;bal += d;

}

void withdrawl(){

int d;cout<<"\nEnter the amount to be withdrawn : ";cin>>d;if(bal<d)cout<<"\n\nU don't have so much amount in UR account..";else{

bal -= d;cout<<"\nAmount balance : "<<bal;

}

void display(){

cout<<"\nName is : "<<name;cout<<"\nAccount number : "<<acc;cout<<"\nAccount type ; "<<type;cout<<"\nBalance is : "<<bal;

Page 343: BOOK on C++ Programs

}};

void main(){

clrscr();bank b;b.getamount();b.deposit();b.withdrawl();b.display();getch();

}

Page 344: BOOK on C++ Programs

// Program to show function overloading in C++.

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

void starline(){

for(int i=0; i<30; i++)cout<<"*"<<" ";

}

void starline(char ch){

cout<<endl;for(int i=0; i<30; i++)

cout<<ch<<" ";} void starline(char ch, int k){

cout<<endl;for(int i=0; i<k; i++)

cout<<ch<<" ";}

void main(){

clrscr();starline();starline('?');starline('-', 30);getch();

}

Page 345: BOOK on C++ Programs

// Program to overload '+' & '-' operator in a single program in c++

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

class distance{

int feet;float inch;

public:distance(){

feet=0;inch=0;

}

void getdata(int f,float i){

feet=f;inch=i;

}

void showdata(){

cout<<"\nFeets Are: "<<feet;cout<<"\nInches Are: "<<inch;

}

distance operator+(distance d){

distance d1;d1.feet=feet+d.feet;d1.inch=inch+d.inch;while(inch>12){

d1.inch-=12;d1.feet++;

}return d1;

}

distance operator-(distance d){

distance d1;inch = inch+(feet*12);d.inch=d.inch+(d.feet*12);if(inch>d.inch)

d1.inch=inch-d.inch;else

d1.inch=d.inch-inch;while(d1.inch>=12){

d1.inch-=12;d1.feet++;

Page 346: BOOK on C++ Programs

}d1.feet+=feet-d.feet;return d1;

}};

void main(){

clrscr();distance dist1,dist2,dist3;dist1.getdata(12,5.6);dist2.getdata(10,4.4);dist1.showdata();dist2.showdata();dist3=dist1+dist2;dist3.showdata();dist3=dist1-dist2;dist3.showdata();getch();

}

Page 347: BOOK on C++ Programs

// Program to show the use of I/O statements

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

void main(){

int num;clrscr();cout<<"Enter any number : ";cin>>num;cout<<"\n The value is : "<<num;getch();

}

Page 348: BOOK on C++ Programs

// Program to show the use of double data type

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

double power (int, int);

void main(){

int n,p;clrscr();cout<<"Enter the number : ";cin>>n;cout<<"Enter the power : ";cin>>p;cout<<endl<<"Result : "<<power(n,p);getch();

}

double power (int a, int b){

for (int i=1; i<b; i++)a *= a;

return(a);}

Page 349: BOOK on C++ Programs

// Program to show the use of double data type

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

int & setx();int x;

void main(){

setx() = 90;clrscr();cout<<"X= "<<x;getch();

}

int & setx(){

return x;}

Page 350: BOOK on C++ Programs

// Program to find the average of an Array using functions

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

float avg (int x){

static int i=0, sum=0;i++;sum += x;return(sum/i);

}

void main(){

float a;int n,r;clrscr();cout<<"How many values U want to enter : ";cin>>r;do{

cout<<"Enter the value : ";cin>>n;a = avg(n);r--;

}while(r != 0);cout<<"Average : "<<a;getch();

}

Page 351: BOOK on C++ Programs

// Program to show the use of structures

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

struct motor{

int model;float cost;char name;

};

void main(){

motor m;clrscr();m.model = 101;m.cost = 50400;m.name = 'A';cout<<"Model No. : "<<m.model<<endl;cout<<"Cost : "<<m.cost<<endl;cout<<"Model Name : "<<m.name;

}

Page 352: BOOK on C++ Programs

// Program to generate a Structures named Phone & insert data into it.

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

struct phone{

int acode;float ecode;char no;

}m,n;

void main(){

clrscr();m.acode = 91;m.ecode = 591;m.no = 249;cout<<"Area Code : "<<m.acode<<endl;cout<<"Exchange Code : "<<m.ecode<<endl;cout<<"Phone No. : "<<m.no;cout<<"\nInsert NOW";cout<<"Enter the Area Code : ";cin>>n.acode;cout<<"Enter the Exchange Code : ";cin>>n.ecode;cout<<"Enter the Phone No. : ";cin>>n.no;cout<<"Area Code : "<<n.acode<<endl;cout<<"Exchange Code : "<<n.ecode<<endl;cout<<"Phone No. : "<<n.no;

}

Page 353: BOOK on C++ Programs

// Program to calculate the Area of Dining Room using structures.

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

struct distance{

int f;float i;

};

struct room{

distance l;distance w;

};

void main(){

clrscr();room d;d.l.f = 12;d.l.i = 8;d.w.f = 10;d.w.i = 6;float l = d.l.f + d.l.i/12;float w = d.w.f + d.w.i/12;float area = l * w;cout<<"Area si : "<<area;getch();

}

Page 354: BOOK on C++ Programs

// Program to calculate the Volume of a Room using structures.

#include<iostream.h>#include<conio.h> struct distance{

int feet;float inch;

}; struct volume{

distance length;distance breadth;distance height;

}; void main(){

clrscr();volume obj;cout<<"Enter Dimensions For Room\n";cin>>obj.length.feet;cin>>obj.length.inch;cin>>obj.breadth.feet;cin>>obj.breadth.inch;cin>>obj.height.feet;cin>>obj.height.inch;float x,y,z,v ;x=obj.length.feet+obj.length.inch/12;y=obj.breadth.feet+obj.breadth.inch/12;z=obj.height.feet+obj.height.inch/12;v=x*y*z;cout<<"\nVolume Of Room Is: "<<v;getch();

}

Page 355: BOOK on C++ Programs

// Program to show the use of Enum data type

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

void main(){

enum direction {east,west,north,south};direction d = south;clrscr();cout<<"Value of d = "<<d;getch();

}

Page 356: BOOK on C++ Programs

// Program to show the use of Enum data type

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

void main(){

enum day {mon,tue,wed,thu,fri,sat,sun};clrscr();day d1 = tue;day d2 = sat;int days = d2 - d1;if (d1 < d2)

cout<<"Day 1 comes"<<days<<"Days Before Day 2";getch();

}

Page 357: BOOK on C++ Programs

// Program to check that entered year is a LEAP and SUPER year or NOT.

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

void main(){

int y;clrscr();cout<<"Enter Any Year " ;cin>>y;if ((y%4==0)&&(y%400!=0))

cout<<"\n Enterd year is a LEAP YEAR ONLY";else if ((y%4==0)&&(y%400==0))

cout<<"\n Enterd year is a LEAP & SUPER YEAR both";else

cout<<"\n Enterd year is not a LEAP YEAR ";getch();

}

Page 358: BOOK on C++ Programs

// Program to show the use break statement

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

void main(){

int num;clrscr();for (int i=0; i<2; i++){

for (int j=0; j<2; j++)if (i==j) break;

cout<<i<<j;}getch();

}

Page 359: BOOK on C++ Programs

// Program to calculate the factorial of a Given number.

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

long int fact(int n){

if(n>0)return(n*fact(n-1));

elsereturn(1);

}

void main(){

int i;clrscr();cout<<"\nEnter a number: ";cin>>i;cout<<"\n\nFactorial of %d is : "<<i<<fact(i);getch();

}

Page 360: BOOK on C++ Programs

// Program to calculate the factorial of a Given number.

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

void main(){

int num, n, sum = 0;clrscr();cout<<"Enter any No. : ";cin>>num;while( num > 0){

n = num % 10;sum += n;num /= 10;

}cout<<"\nSum of Digits : "<<sum;getch();

}

Page 361: BOOK on C++ Programs

// Program to generate the Fabonacii Series upto a Given Range.

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

void main(){

int a=1, b=1, s=0, n;clrscr();cout<<"How many terms U want to generate : ";cin>>n;cout<<a<<" "<<b;while ((n-2)!=0){

s = a + b;cout<<" "<<s;a = b;b = s;n--;

}getch();

}

Page 362: BOOK on C++ Programs

// Program to check the number is PERFECT, ABUNDANT or DEFICIENT.

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

void main(){

int num, sum=0;clrscr();cout<<"Enter any No. : ";cin>>num;for (int i=1; i<=num; i++)

if ((num%i)==0)sum += i;

if (sum==num)cout<<"\n Entered No. is PERFECT";

else if (sum<num)cout<<"\n Entered No. is ABUNDANT";

elsecout<<"\n Entered No. is DEFICIENT";

getch();}

Page 363: BOOK on C++ Programs

// Program to calculate No. of days from Ist JAN to Entered Date.

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

void main(){

int mo,da,sa;int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};clrscr();cout<<"Enter the Month : ";cin>>mo;cout<<"Enter the Day : ";cin>>da;sa = da;for (int i=1; i<mo; i++)

sa += a[i-1];cout<<"Total Days are : "<<sa;getch();

}

Page 364: BOOK on C++ Programs

// Program to calculate the product of two matrices of 3X3

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

void main(){

int a[3][3], b[3][3], p[3][3];int i, j, k, s;clrscr();cout<<"Enter elements of first matrix \n";for(i=0; i<3; i++)

for(j=0; j<3; j++)cin>>a[i][j];

cout<<"Enter elements of second matrix \n";for(i=0; i<3; i++)

for(j=0; j<3; j++)cin>>b[i][j];

cout<<"\n\nfirst matrix \n";for(i=0; i<3; i++){

for(j=0; j<3; j++)cout<<a[i][j];

cout<<"\n";}cout<<"\n\nsecond matrix \n";for(i=0; i<3; i++){

for(j=0; j<3; j++)cout<<b[i][j];

cout<<"\n";}//product startsfor(i=0; i<3; i++)

for(j=0; j<3; j++){

s = 0;for(k=0; k<3; k++)

s = s + a[i][k] * b[k][j];p[i][j] = s;

}cout<<"\n\nProduct result\n";for(i=0; i<3; i++){

for(j=0; j<3; j++)cout<<p[i][j];

cout<<"\n";}

}

Page 365: BOOK on C++ Programs

// Program to Find Maximum No. in an Array by passing it to a function.

# include <iostream.h># include <conio.h>#define max 100

void max1 (int a[], int n){

int ma = -1;

for (int i=0; i<n; i++)if (ma < a[i])

ma = a[i];cout<<"Maximaun No. in Array is : "<<ma;

}

void main(){

int s,a[max],i,result;clrscr();cout<<"Enter the Range : ";cin>>s;for (i=0; i<s; i++){

cout<<"Enter the "<<i+1<<" no. : ";cin>>a[i];

}max1(a,s);getch();

}

Page 366: BOOK on C++ Programs

// Program to Convert Entered No. into Words

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

void main(){

int digit [10], num, temp, i, j;clrscr();cout<<"Enter any no. : ";cin>>num;temp = num;i = 0;while (num != 0){

digit[i++] = (num-num/10*10);num = num/10;

}j = --i;cout<<"Entered No. in Words is : "<<endl;for (int k=j; k>=0; k--){

switch(digit[k]){

case 1 :cout<<"One";break;

case 2 :cout<<"Two";break;

case 3 :cout<<"Three";break;

case 4 :cout<<"Four";break;

case 5 :cout<<"Five";break;

case 6 :cout<<"Six";break;

case 7 :cout<<"Seven";break;

case 8 :cout<<"Eight";break;

case 9 :cout<<"Nine";break;

case 0 :cout<<"Zero";break;

}

Page 367: BOOK on C++ Programs

cout<<endl;getch();

}}

Page 368: BOOK on C++ Programs

// Program to show the use continue statement

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

void main(){

clrscr();for (int i=0; i<2; i++){

for (int j=0; j<2; j++){

if (i==j)continue;

cout<<i<<j;}

}getch();

}

Page 369: BOOK on C++ Programs

// Program to show the use GOTO statement

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

void main(){

int run;clrscr();cout<<"Enter the runs of India : ";cin>>run;if (run > 300)

goto a;else

cout<<"\n We want more RUNS ";a:

cout<<endl<<"It is difficult to defeat Australia ";getch();

}

Page 370: BOOK on C++ Programs

// Program to convert the distance in Mtrs. into Kms. using functions

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

int convert (int k){

int m;m = k * 1000;return m;

}

void main(){

int km,mt;clrscr();cout<<"Enter the distance in Kms. : ";cin>>km;mt = convert(km);cout<<endl<<"Equivalent Meters are : "<<mt;getch();

}

Page 371: BOOK on C++ Programs

// Program to convert the distance in Mtrs. into Kms. using functions

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

int convert (int);

void main(){

int km,mt;clrscr();cout<<"Enter the distance in Kms. : ";cin>>km;mt = convert(km);cout<<endl<<"Equivalent Meters are : "<<mt;getch();

}

int convert (int k){

return(k * 1000);}

Page 372: BOOK on C++ Programs

// Program to swap TWO numbers using Call By Reference

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

void swap (int & , int &);

void main(){

int num1, num2;clrscr();cout<<"Enter the value of num1 : ";cin>>num1;cout<<"Enter the value of num2 : ";cin>>num2;swap(num1,num2);cout<<"\n\n After swapping : ";cout<<endl<<"num1 = "<<num1;cout<<endl<<"num2 = "<<num2;getch();

}

void swap (int &n1, int &n2){

int t;t = n1;n1 = n2;n2 = t;

}

Page 373: BOOK on C++ Programs

// Program to convert the distance in Mtrs. into Kms. using Inline functions

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

inline float convert (int k){

int m;m = k * 1000;return m;

}

void main(){

int km,mt;clrscr();cout<<"Enter the distance in Kms. : ";cin>>km;mt = convert(km);cout<<endl<<"Distance in Meters are : "<<mt;getch();

}

Page 374: BOOK on C++ Programs

// Program to calculate the Area of Circle using functions

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

float circlear (float);

void main(){

float r;clrscr();cout<<"Enter the radius : ";cin>>r;cout<<endl<<"The Area of Circle is : "<<circlear(r);getch();

}

float circlear (float a){

return(a * a * 3.14);}

Page 375: BOOK on C++ Programs

// Program to show the use of structure.

#include<iostream.h>#include<conio.h> struct phone{

int c_code;long int city_code;long int num;

}; void main(){

clrscr();phone obj1,obj2;obj1.c_code=91;obj1.city_code=591;obj1.num=488888;cout<<"\nEnter Country Code :\n ";cin>>obj2.c_code;cout<<"\nEnter City Code : ";cin>>obj2.city_code;cout<<"\nEnter Phone Number : ";cin>>obj2.num;cout<<"\n Number Is: "<<obj1.c_code;cout<<" " <<obj1.city_code<<" "<< obj1.num;cout<<"\n Number Is : "<<obj2.c_code;cout<<" "<< obj2.city_code<<" "<< obj2.num;getch();

}

Page 376: BOOK on C++ Programs

// Program to convert object of one class type to another.

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

class rec{

float xco;float yco;public:

rec(){

xco=0;yco=0;

}rec(float x,float y){

xco=x;yco=y;

}dispaly(){

cout<<"\nValue Of X: "<<xco;cout<<"\nValue Of Y: "<<yco;

}};

class polar{

float radius;float angle;public:

polar(){

radius=0.0;angle=0.0;

}polar(float r,float a){

radius=r;angle=a;

}display(){

cout<<"\nRadius Is: "<<radius;cout<<"\nAngle Is : "<<angle;

}operator rec(){

float x=radius*cos(angle);float y=radius*sin(angle);return rec(x,y);

}};

Page 377: BOOK on C++ Programs

void main(){

clrscr();polar p(10,.78);rec r;r=p;r.dispaly();p.display();getch();

}

Page 378: BOOK on C++ Programs

// Program to depict the use of strings in classes.

#include<iostream.h>#include<conio.h>#include<string.h>const int MAX=100;

class string{

char str[MAX];public:

string(){

str[0]='\0';}string(char s[]){

strcpy(str,s);}void display(){

cout<<"String Is: "<<str;}void concat(string s2){

if((strlen(str)+strlen(s2.str))<MAX)strcat(str,s2.str);

elsecout<<"\n String Is Too Long\n";

}};

void main(){

clrscr(); string s1("Good Morning");string s2="Good Night";string s3;cout<<"\nS1 = ";s1.display();cout<<"\nS2 = ";s2.display();cout<<"\nS3 = ";s3.display();s3=s1;cout<<"\nS3 = ";s3.display();s3.concat(s2);cout<<"\nS3 = ";s3.display();getch();

}

Page 379: BOOK on C++ Programs

// Program to copy two objects using operator overloading.

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

class abc{

int x;float y;public:

abc(){

x=0;y=0;

}void getdata(){

x=9;y=2.55;

}void operator=(abc z){

x=z.x;y=z.y;

}void display(){

cout<<"\nx : "<<x;cout<<"\ny : "<<y;

}

};

void main(){

clrscr();abc obj1,obj2;obj1.getdata();obj2=obj1;obj1.display();obj2.display();getch();

}

Page 380: BOOK on C++ Programs

// Program to perform all arithmetic operations along with ++, -- & == operator overloading.

#include<iostream.h>#include<conio.h>class counter{

int count;public:

counter(){

count=0;}void getcount(){

cout<<"\nCount= ";cin>>count;

}void putcount()

{cout<<count;}

void operator++(){

count++;}void operator--(){

count--;}void operator=(counter m){

count = m.count;}void operator*(counter m)

{count*= m.count;}int operator==(counter( m)){if(count==m.count)return 1;else return 0;}};void main(){

clrscr();counter c1,c2;c1.getcount();c1++;c1.putcount();

c1--;c1.putcount();c2=c1;c2.putcount();

Page 381: BOOK on C++ Programs

if(c1==c2)cout<<"Equal";elsecout<<"Not Equal";counter c3;c1*c2;

c1.putcount();getch(); }