C++ programs :-

|
|
/*CONVERSION OF DECIMAL NUMBER TO BINARY
NUMBER*/ #include<iostream.h> #include<math.h> void main() { int b=0,d,i,n; cout<<"enter the decimal no\n"; cin>>d; for(i=0;d!=0;d/=2,i++) {b+=(d%2)*pow(10,i); } cout<<"the binary no. is\n"<<b; } |
|
|
/*SORTING OF ELEMENTS IN AN ARRAY*/ #include<iostream.h> #include<conio.h> void main() { clrscr(); const int n=10; int arr[n],temp,s,k,smallest,loc; cout<<"ENTER THE "<<n<<" ELEMENTS SEPARATED BY SPACE\n"; for(int j=0;j<n;j++) cin>>arr[j]; cout<<"\nTHE ELEMENTS ENTERED ARE :"<<'\n'; for(k=0;k<n;k++) cout<<arr[k]<<" "; /*THIS SORTS THE ELEMENT*/ for(k=0;k<n-1;k++) { smallest=arr[k];loc=k; for(j=k;j<n;j++) { if(smallest>arr[j]) { smallest=arr[j];loc=j; } } temp=arr[k]; arr[k]=arr[loc]; arr[loc]=temp; } //**PRINT THE ARRAY** cout<<"\n\n\nTHE SORTED ARRAY IS"<<'\t'<<'\t'<<'\n'; for(s=0;s<n;s++) cout<<arr[s]<<" "; char ch;cout<<"\n\n\nPRESS ANY KEY AND ENTER TO EXIT";cin>>ch; } //**END OF PROGRAM** |
|
|
/*PROGRAM TO GENERATE THE FIBONACCI SERIES*/ #include<iostream.h> #include<conio.h> void main() {clrscr(); int first,second,third,n; first=0; second=1; cout<<"upto how many elements would you like to generate the fibonacci series"; cin>>n; cout<<"\nFIBONACCI SERIES\n"; cout<<first<<endl<<second; for(int i=2;i<n;++i) { third=first+second; cout<<endl<<third<<endl; first=second; second=third; } } |
|
|
/*PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS
PALINDROME OR NOT*/ #include<iostream.h> #include<conio.h> void main() {clrscr(); int n,num,digit,rev=0; cout<<"input the number\n"; cin>>num; n=num; do {digit=num%10; rev=rev*10+digit; num/=10; }while(num!=0); cout<<"the reverse of the number is "<<rev<<'\n'; if(n==rev) cout<<"the number is palindrome\n"; else cout<<"not palindrome"; } |
|
|
/*PROGRAM TO FIND WHETHER GIVEN NUMBER IS ODD
OR EVEN OR PRIME*/ #include<iostream.h> #include<conio.h> void main() {clrscr(); char ch; int num,i; do { cout<<"enter the number \n"; cin>>num; if(num%2==0) cout<<"even\n"; else cout<<"odd\n"; if(num==1||num==2) cout<<"prime\n"; else { for(i=3;i<=num/2;++i) if(num%i==0) break; if(i>num/2) cout<<"prime\n"; } cout<<"continue(y/n)"; cin>>ch; }while(ch=='y'); } |
|
|
/*PROGRAM TO FIND THE FACTORIAL OF A GIVEN
NUMBER*/ #include<iostream.h> #include<conio.h> void main() {clrscr(); int num,fact=1,n; cout<<"enter the number\n"; cin>>num; n=num; while(num) {fact*=num; --num; } cout<<"the factorial of "<<n<<" is "<<fact; } |
|
|
/*PROGRAM TO CHECK WHTHER A NUMBER IS PERFECT
OR NOT*/ #include<iostream.h> #include<conio.h> void main() {clrscr(); int a,b,i,sum=1; cout<<"enter a number\n"; cin>>a; for(i=2;i<=a;i++) {b=a%i; if(b==0) {sum+=i; } } if(sum==a) cout<<"perfect number\n"; else cout<<" not a perfect no"; } |
|
|
/*PROGRAM TO SHOW THE USE OF FUNCTION
OVERLOADING IN CALCULATION OF AREA OF A TRIANGLE OR RECTANGLE OR SQUARE*/ #include<iostream.h> #include<conio.h> #include<math.h> float area(float a,float b,float c) { float s,ar; s=(a+b+c)/2; ar=sqrt(s*(s-a)*(s-b)*(s-c)); return ar; } float area(float a,float b) {return a*b; } float area(float a) {return a*a; } void main() {clrscr(); int ch,s1,s2,s3,ar; do { cout<<"\narea main menu\n"; cout<<"1.triangle"; cout<<"\n2.rectangle"; cout<<"\n3.square"; cout<<"\n4.exit"; cout<<"\nenter your choice(1-4)"; cin>>ch; switch(ch) {case 1:cout<<"\nenter 3 sides"; cin>>s1>>s2>>s3; ar=area(s1,s2,s3); cout<<"\nthe area is"<<ar; break; case 2:cout<<"\nenter two sides"; cin>>s1>>s2; ar=area(s1,s2); cout<<"\narea="<<ar; break; case 3:cout<<"\nenter a side"; cin>>s1; ar=area(s1); cout<<"\narea="<<ar; break; } } while(ch!=4); } |
|
|
/*PROGRAM TO CONVERT A GIVEN TEMPERATURE
SCALE*/ #include<iostream.h> #include<conio.h> void main() {clrscr(); int ch; float temp,c; cout<<"\ntemperature conversion menu"; cout<<"\n1.fahrenheit to celsius"; cout<<"\n2.celcius to farenheit"; cout<<"\n3.exit"; cout<<"\nenter your choice"; cin>>ch; if(ch==1) {cout<<"\nenter the temp in fahrenheit"; cin>>temp; c=(temp-32)/1.8; cout<<c<<"celsius"; } if(ch==2) { cout<<"\nenter in celsius"; cin>>temp; c=1.8*temp+32; cout<<c<<"fahrenheit"; } } |
|
|
/*PROGRAM TO INPUT CHARACTERS AND CHANGE
THEIR CASE*/ #include<iostream.h> #include<conio.h> #include<stdio.h> void main() {clrscr(); char ch; do {cout<<"\nenter a character "; ch=getchar(); if(ch=='\n') ch=getchar(); if(ch>=65&&ch<=90) ch+=32; else if(ch>=97&&ch<=122) ch-=32; putchar(ch); cout<<endl; cout<<"press 0 to discontinue"; }while(ch!='0'); } |
|
|
//CONVERSION OF OCTAL NUMBER TO BINARY
NUMBER// #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int d=0,i,o; cout<<"enter the octal number\n"; cin>>o; for(i=0;o!=0;o/=10,i++) {d+=(o%10)*pow(8,i); } int b=0; for(int j=0;d!=0;d/=2,j++) {b+=(d%2)*pow(10,j); } cout<<"the binary no. is"<<b; } |
|
|
/*PROGRAM TO SHOW THE USE OF GRAPHICS IN C++
*/ #include<graphics.h> #include<conio.h> #include<dos.h> const int w=75; class rect { int xco,yco; int linecolor,fillcolor; public: rect() {xco=0;yco=0;linecolor=WHITE;fillcolor=WHITE;} void set(int x,int y,int lc, int fc) {xco=x;yco=y;linecolor=lc;fillcolor=fc;} void draw() {setcolor(linecolor); setlinestyle(SOLID_LINE,0,THICK_WIDTH); rectangle(xco-w,yco-w,xco+w,yco+w); setfillstyle(SOLID_FILL,fillcolor); floodfill(xco,yco,linecolor); line(xco-w,yco-w,xco+w,yco+w); } }; void main() { int driver,mode; driver=DETECT; initgraph(&driver,&mode,"\\tc\\bgi"); rect r1; rect r2; rect r3; r1.set(80,150,YELLOW,RED); r2.set(250,150,YELLOW,GREEN); r3.set(420,150,YELLOW,BLUE); r1.draw(); r2.draw(); r3.draw(); sound(5000);delay(2000);nosound(); getch(); closegraph(); } |
|
|
//*PROGRAM SHOWING THE USE OF STRUCTURES*// #include<iostream.h> #include<conio.h> #include<stdio.h> //regno structure declared struct regno { int area_code; int region_code; int school_code; short current_year; int serial_no; }; //student structure declared struct student { char name[20]; char category; int marks[5]; regno registration_no; }arr[2]; void main() { char ch; clrscr(); //input for(int i=1;i<=2;i++) //i is the counter for inputting { cout<<"enter the name"<<endl; // cin.get(ch); // cin.getline(arr[i].name,20); gets(arr[i].name); cout<<"enter the category"<<endl; cin>>arr[i].category; for(int j=1;j<=2;j++) { cout<<"enter marks"<<endl; cin>>arr[i].marks[j]; } cout<<"enter registration code"<<endl; cin>>arr[i].registration_no.area_code; cout<<"enter region code"<<endl; cin>>arr[i].registration_no.region_code; cout<<endl; } //output clrscr(); for(int k=1;k<=2;k++) //k is the counter for displaying result { cout<<endl<<"name ="; cout.write(arr[k].name,20); cout<<"category ="<<arr[k].category<<endl; for(int r=1;r<=2;r++) { cout<<"marks ="<<arr[k].marks[r]<<endl; } cout<<"area code ="<<arr[k].registration_no.area_code<<endl; cout<<"region code ="<<arr[k].registration_no.region_code<<endl; //cin>>ch; } } |
|
|
//*DISPLAY ODD NOS. FROM 1 TO 10 IN THE FORM
OF A PYRAMID*// #include<conio.h> #include<iostream.h> void main() { clrscr (); int i,j; for(i=1;i<=9;i+=2) { for(j=1;j<=i;j+=2) { cout<<j<<'\t'; } cout<<'\n'; } } |
|
|
/*PROGRAM TO CONCATENATE TWO STRINGS TO FIND LENGTH OF A STRING AND TO REVERSE A STRING*/ #include<iostream.h> #include<conio.h> class string { char a[20]; public: void initialise() { cout<<"\nenter the string\n "<<endl; cin.getline(a,20); } void display() { cout<<endl; cout.write(a,20); } void reverse(); void concatenate(); }; void string::concatenate() { int i,j,k; char b[20],c[20]; for(i=0;i<20;i++) c[i]=' '; initialise(); cout<<"enter the second string\n "; cin.getline(b,20); for(i=0;a[i]!=0;i++) c[i]=a[i]; for(k=0;b[k]!=0;k++) c[i+k]=b[k]; c[i+k]='\0'; cout<<"\nconcatenated string is\n "; cout.write(c,20); } void string::reverse() { initialise(); int len,i,j,k; for(i=0;a[i]!='\0';i++); cout<<"the length of the string is "<<i; char b[20]; cout<<"\nthe reversed string is\n"; for(j=0,k=i-1;k>=0;k--,j++) { b[j]=a[k]; cout<<b[j]; } } void main() { clrscr(); string str; str.concatenate(); str.reverse(); } |
|
|
/*PROGRAM TO SORT CITY NAME AND POPULATION
ACCORDING TO PINCODE*/ #include<iostream.h> #include<conio.h> //sort city_name struct city { char c_name[20]; int pop; int pc; }sp[5]; void main() { clrscr(); int t; char ch,org[5]; for(int i=0;i<3;i++) {cout<<"\nenter the pincode of the city ="; cin>>sp[i].pc; org[i]=sp[i].pc; cout<<"\nenter the name of the city ="; cin.get(ch); cin.getline(sp[i].c_name,20); cout<<"\nenter the population ="; cin>>sp[i].pop; } //sort the pincode for(int j=0;j<3;j++) {for(int k=0;k<2;k++) {if(sp[k].pc>sp[k+1].pc) {t=sp[k].pc; sp[k].pc=sp[k+1].pc; sp[k+1].pc=t; } } } //displaying clrscr(); cout<<"sorted output "; for(int m=0;m<3;m++) {cout<<"\npincode ="<<sp[m].pc<<"\t"; for(int n=0;n<3;n++) {if(sp[m].pc==org[n]) {cout<<"city name ="<<sp[n].c_name<<"\t"; cout<<"population is ="<<sp[n].pop; } } } } |
|
|
/*PROGRAM SHOWING THE USE OF STATIC FUNCTION
WITHIN CLASSES*/ #include<iostream.h> #include<conio.h> class x { int codeno; float price; static int count; public: void getval(int i,float j) {codeno=i; price=j; ++count; } void display(void) { cout<<"codeno="<<codeno<<"\t"; cout<<"price= "<<price<<"\n"; } static void dispcount(void) {cout<<"count= "<<count; } }; int x::count=0; void main() { clrscr(); x o1,o2; o1.getval(111,25.64); o2.getval(102,434.87); x::dispcount; x o3; o3.getval(214,65.84); o1.display(); o2.display(); o3.display(); } |
|
|
/*PROGRAM SHOWING WORKING OF CLASSES TO CALCULATE
LARGEST ITEM PRICE TO DISPLAY LIST OF ITEMS TO FIND THE SUM OF THE ITEM PRICES*/ #include<iostream.h> #include<conio.h> class item { int item_code[5]; float it_price[5]; public: void initialise(); float largest(void); float sum(void); float display_items(void); }; void item::initialise() { cout<<"enter the item code and price for 5 items"<<endl; for(int i=0;i<5;i++) { cin>>item_code[i]; cin>>it_price[i]; } }; float item::largest(void) { int i=0; float large=it_price[i]; for(i=1;i<5;i++) { if(large<it_price[i]) large=it_price[i]; } return large; } float item::sum(void) { float sum=0; for(int i=0;i<5;i++) {sum+=it_price[i]; } return sum; } float item::display_items(void) { cout<<"code price"<<endl; for(int i=0;i<=5;i++) {cout<<item_code[i]; cout<<endl<<it_price<<endl; } return 0; } void main() { clrscr(); item order; order.initialise(); float total,biggest; int ch=0; do { clrscr(); cout<<"main menu"<<endl; cout<<"1.largest price"<<endl; cout<<"2.sum"<<endl; cout<<"3.display item"<<endl; cout<<"4.exit"<<endl; cin>>ch; switch(ch) { case 1:biggest=order.largest(); cout<<"the largest is"<<biggest<<endl; break; case 2:total=order.sum(); cout<<"the total is"<<total<<endl; break; case 3:float display=order.display_items(); cout<<display; break; case 4:break; default: cout<<"wrong choice"; } }while(ch>=1&&ch<=3); }
|