rabbinur

Practice session (Program)

Aug 7th, 2012
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.23 KB | None | 0 0
  1. (1)
  2. Program to add two numbers
  3. #include<iostream.h>
  4. #include<conio.h>
  5. void main()
  6. {
  7. int a,b,sum;
  8. cout<<”Enter two numbers”;
  9. cin>>a>>b;
  10. sum=a+b;
  11. cout<<”The  sum is”<<sum;
  12. getch();
  13. }
  14.  
  15. (2)
  16. Program to Convert a Decimal number upto 100 digits into Binary number
  17. #include<iostream.h>
  18. #include<conio.h>
  19. #include<string.h>
  20. #include<stdio.h>
  21.  
  22.  
  23. void main()
  24. {
  25. clrscr();
  26. char dec[101],bin[101];
  27. int n,q,rem,t,j,k=0;
  28. cout<<"Enter Decimal number upto 100 digits";
  29. gets(dec);
  30. while(dec[0]!='\0')
  31. {
  32.     rem=0;j=0;
  33.     for(int i=0;dec[i]!='\0';i++)
  34.         {
  35.             t=dec[i]-48;
  36.             n=rem*10+t;
  37.             q=n/2;
  38.             rem=n%2;
  39.             dec[j]=q+48;
  40.             j++;
  41.             if(dec[0]=='0')
  42.                 j=0;
  43.  
  44.         }
  45.         dec[j]='\0';
  46.         bin[k]=rem+48;
  47.         k++;
  48. }
  49. bin[k]='\0';
  50. strrev(bin);
  51. cout<<"The binary Number is";
  52. puts(bin);
  53. getch();
  54. }
  55.  
  56. (3)
  57. Function Overloading Example
  58. #include<iostream.h>
  59. #include<conio.h>
  60. #include<math.h>
  61.  
  62.  
  63.  
  64. void area(int a)
  65. {
  66.     cout<<"Area of Square"<<a*a;
  67. }
  68. void area(int l,int b)
  69. {
  70.     cout<<"Area of rectangle"<<l*b;
  71. }
  72. void area(int a,int b,int c)
  73. {
  74.     float s,ar;
  75.     s=(a+b+c)/2.0;
  76.     ar=sqrt(s*(s-a)*(s-b)*(s-c));
  77.     cout<<"Area of triangle"<<ar;
  78. }
  79. void main()
  80. {
  81. int op,a,b,c;
  82. cout<<"1.Area of Square";
  83. cout<<"\n2.Area of Rectangle";
  84. cout<<"\n3.Area of Triangle";
  85. cout<<"\nEnter your option";
  86. cin>>op;
  87. switch(op)
  88.     {
  89.     case 1:
  90.             cout<<"Enter Side";
  91.             cin>>a;
  92.             area(a);
  93.             break;
  94.     case 2:
  95.             cout<<"Enter length and breadth";
  96.             cin>>a>>b;
  97.             area(a,b);
  98.             break;
  99.     case 3:
  100.             cout<<"Enter Sides of a triangle";
  101.             cin>>a>>b>>c;
  102.             area(a,b,c);
  103.             break;
  104.     default:
  105.             cout<<"Invalid Option";
  106.     }
  107. getch();
  108. }
  109.  
  110. (4)
  111. Program to add 10 integer numbers from 1 to 10 using while loop
  112. #include<iostream.h>
  113. #include<conio.h>
  114. void main()
  115. {
  116. clrscr();
  117. int x=1,n=10,sum=0;
  118. while(x<=n)
  119.     {
  120.         sum=sum+x;
  121.         x++;
  122.     }
  123. cout<<"TOTAL is "<<sum;
  124. getch();
  125. }
  126.  
  127. (5)
  128.  
  129. Program to add 10 integer numbers from 1 to 10 using do...while loop
  130. #include<iostream.h>
  131. #include<conio.h>
  132. void main()
  133. {
  134. clrscr();
  135. int x=1,n=10,sum=0;
  136. do
  137.     {
  138.         sum=sum+x;
  139.         x++;
  140.     }while(x<=n);
  141.     cout<<"TOTAL is "<<sum;
  142. getch();
  143. }
  144.  
  145. (6)
  146. Program to allot Grade to students on the basis of their percentage
  147. #include<iostream.h>
  148. #include<conio.h>
  149. void main()
  150. {
  151. clrscr();
  152. int hindi,english,maths,total,percent;
  153. cout<<"enter hindi marks ";
  154. cin>>hindi;
  155. cout<<"enter english marks ";
  156. cin>>english;
  157. cout<<"enter maths marks ";
  158. cin>>maths;
  159. total= hindi + english + maths;
  160. cout<<"total is "<<total<<endl;
  161. percent = total/3;       /*switch case can not be used for conditions*/
  162. if(percent >= 90 && percent <=100)
  163.   {    cout<<"Excellent ";  }
  164. else if(percent >= 80 && percent <= 89)
  165.     {cout<<"A+ ";                }
  166. else if(percent >= 70 && percent <= 79)
  167.     {cout<<"A ";  }
  168. else if(percent >= 60 && percent <= 69)
  169.   {    cout<<"B+ ";                }
  170. else if(percent >= 50 && percent <= 59)
  171.     {cout<<"B ";  }
  172. else if(percent >= 40 && percent <= 49)
  173.   {    cout<<"C ";    }
  174. else if(percent >= 0 && percent <= 39)
  175.     {cout<<"F "; }
  176. getch();
  177.  
  178. (7)
  179.  
  180. Program to find maximum of three numbers
  181. #include<iostream.h>
  182. #include<conio.h>
  183. int maximum(int a,int b, int c);
  184. void main()
  185. {
  186.     int x,y,z,max;
  187.     cout<<"enter three numbers ";
  188.     cin>>x>>y>>z;
  189.     max=maximum(x,y,z);
  190.     cout<<"maximum is "<<max;
  191.     getch();
  192. }
  193. int maximum(int a,int b,int c)
  194. {
  195.     if(a>b)
  196.         if(a>c)
  197.             return a;
  198.         else
  199.             return c;
  200.  
  201.     else
  202.         if(b>c)
  203.             return b;
  204.         else
  205.             return c;
  206.  
  207. }
  208.  
  209. (8)
  210.  
  211. Program to perform arithmetic operations
  212. #include<iostream.h>
  213. #include<conio.h>
  214. #include<stdlib.h> //for exit function
  215. #include<dos.h> //for sleep function
  216. double operation(float, float ,char);
  217. void main()
  218. {
  219.     clrscr();
  220.     float x,y,z;
  221.     char op;
  222.     cout<<"enter first value ";
  223.     cin>>x;
  224.     cout<<"enter second value ";
  225.     cin>>y;
  226.     cout<<"enter operator ";
  227.     cin>>op;
  228.     z=operation(x,y,op);
  229.     cout<<z;
  230.     getch();
  231. }
  232. double operation(float a,float b, char op)
  233. {
  234.     float c;
  235.     switch(op)
  236.     {
  237.         case '+' :  c=a+b;
  238.                         break;
  239.         case '-' :  c=a-b;
  240.                         break;
  241.         case '*' :  c=a*b;
  242.                         break;
  243.         case '/' :  if(b==0)
  244.                 {
  245.                     cout<<"wrong values";
  246.                     sleep(5);
  247.                     exit(0);
  248.                 }
  249.                 else
  250.                 c=a/b;
  251.                 break;
  252.         case 'q' :  exit(0);
  253.         default  :  cout<<"worng operator";
  254.                         break;
  255.     }
  256.     return c;
  257. }
  258.  
  259. (9)
  260.  
  261. Program to check maximum number using function and pointer
  262. #include<iostream.h>
  263. #include<conio.h>
  264. long int max(int *,int *);
  265. void main()
  266. {
  267. clrscr();
  268. int *p,a,b;
  269. cout<<"ENter two values ";
  270. cin>>a>>b;
  271. *p=(a,b);
  272. cout<<"MAximum is "<<*p;
  273. getch();
  274. }
  275. long int max(int *x,int *y)
  276. {
  277. if(*x>*y)
  278.     return *x;
  279. else
  280.     return *y;
  281.  
  282. }
  283.  
  284. (10)
  285.  
  286. Program to find sum of two numbers using "functions"
  287. #include<iostream.h>
  288. #include<conio.h>
  289. void sum(int a,int b);
  290. void main()
  291. {
  292. clrscr();
  293. int a,b;
  294. cout<<"enter 2 vaues";
  295. cin>>a>>b;
  296. sum(a,b);
  297. getch();
  298. }
  299. void sum(int a,int b)
  300.  {
  301.   int sum=0;
  302.   sum=a+b;
  303.   cout<<"sum is"<<sum;
  304.  }
  305.  
  306. (11)
  307.  
  308. Program to find Factorial of number
  309. #include<iostream.h>
  310. #include<conio.h>
  311. void factorial(int a);
  312. void main()
  313. {
  314. clrscr();
  315. int a;
  316. cout<<"enter a no.";
  317. cin>>a;
  318. factorial(a);
  319. getch();
  320. }
  321. void factorial(int a)
  322.  {
  323.   int fact=1;
  324.   for(int i=1;i<=a;i++)
  325.      {
  326.       fact=fact*i;
  327.      }
  328. cout<<"factorial is"<<fact;
  329.  }
  330.  
  331. (12)
  332.  
  333. Program to find whether a number is Prime or not
  334. #include<iostream.h>
  335. #include<conio.h>
  336. void prime(int a);
  337. void main()
  338. {
  339. clrscr();
  340. int a;
  341. cout<<"enter a no.";
  342. cin>>a;
  343. if(a==1)
  344.   {
  345.    cout<<"1 is neither prime nor composite";
  346.   }
  347. prime(a);
  348. getch();
  349. }
  350. void prime(int a)
  351. {
  352. int flag=0;
  353. for(int i=2;i<a;i++)
  354.    {
  355.     if(a%i==0)
  356.       {
  357.        flag=1;
  358.        break;
  359.       }
  360.    }
  361. if(flag==0)
  362.   {
  363.    cout<<"prime no.";
  364.   }
  365. else
  366.   {
  367.    cout<<"not a prime no.";
  368.   }
  369. }
  370.  
  371. (13)
  372.  
  373. Program to check whether a number is Palindrome or not
  374. #include<iostream.h>
  375. #include<conio.h>
  376. void palindrome(int a);
  377. void main()
  378. {
  379. clrscr();
  380. int a;
  381. cout<<"enter a no.";
  382. cin>>a;
  383. palindrome(a);
  384. getch();
  385. }
  386. void palindrome(int a)
  387. {
  388.  int r,b=0,f=100,on=a;
  389.  while(a>0)
  390.     {
  391.      r=a%10;
  392.      a=a/10;
  393.      b=b+(r*f);
  394.      f=f/10;
  395.     }
  396. if(on==b)
  397.   {
  398.    cout<<"palindrome";
  399.   }
  400. else
  401.  {
  402.   cout<<"not a palindrome";
  403.  }
  404. }
  405.  
  406. (13)
  407.  
  408. Program to check whether an alphabet is a Vowel or not
  409. #include<iostream.h>
  410. #include<conio.h>
  411. void vowel(char a);
  412. void main()
  413. {
  414. clrscr();
  415. char a;
  416. cout<<"enter a alphabet";
  417. cin>>a;
  418. vowel(a);
  419. getch();
  420. }
  421. void vowel(char a)
  422. {
  423.  if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
  424.    {
  425.     cout<<"vowel";
  426.    }
  427.  else
  428.   {
  429.    cout<<"not a vowel";
  430.   }
  431. }
  432.  
  433. (14)
  434.  
  435. Program to find maximum in three numbers
  436. #include<iostream.h>
  437. #include<conio.h>
  438. void greater(int a,int b,int c);
  439. void main()
  440. {
  441. clrscr();
  442. int a,b,c;
  443. cout<<"enter 3 nos";
  444. cin>>a>>b>>c;
  445. greater(a,b,c);
  446. getch();
  447. }
  448. void greater(int a,int b,int c)
  449.   {
  450.    if(a>b&&a>c)
  451.      {
  452.       cout<<a<<"is greater";
  453.      }
  454.    else
  455.    {
  456.     if(b>c)
  457.       {
  458.        cout<<b<<" is greater";
  459.       }
  460.     else
  461.       {
  462.        cout<<c<<" is greater";
  463.  
  464.       }
  465.    }
  466. }
  467.  
  468. (15)
  469.  
  470. Program to check whether an year is a leap year or not
  471. #include<iostream.h>
  472. #include<conio.h>
  473. void lyear(int a);
  474. void main()
  475. {
  476. clrscr();
  477. int a;
  478. cout<<"enter a no.";
  479. cin>>a;
  480. lyear(a);
  481. getch();
  482. }
  483. void lyear(int a)
  484.  {
  485.    cout<<"\n\n";
  486.   if(a%100==0&&a%400==0||a%4==0)
  487.     {
  488.      cout<<a<<"is a leap year";
  489.     }
  490.   else
  491.     {
  492.      cout<<a<<"it is not a leap year";
  493.     }
  494. }
  495.  
  496. (16)
  497.  
  498. Program to Swap two numbers
  499. #include<iostream.h>
  500. #include<conio.h>
  501. void swap(int a,int b);
  502. void main()
  503. {
  504. clrscr();
  505. int a,b;
  506. cout<<"enter 2 nos";
  507. cout<<"\n\n";
  508. cin>>a;
  509. cout<<"\n\n";
  510. cin>>b;
  511. cout<<"\n\n";
  512. cout<<"before swapping"<<"\n\n"<<"a="<<a;
  513. cout<<"\n\n";
  514. cout<<"b="<<b;
  515. swap(a,b);
  516. getch();
  517. }
  518. void swap(int a,int b)
  519.      {
  520.        cout<<"\n\n";
  521.        int temp;
  522.        temp=a;
  523.        a=b;
  524.        b=temp;
  525.        cout<<"after swapping"<<"\n\n"<<"a="<<a;
  526.        cout<<"\n\n";
  527.        cout<<"b="<<b;
  528.      }
  529.  
  530. (17)
  531.  
  532. Program to illustrate RETURN BY REFERENCE
  533. #include<iostream.h>
  534. #include<conio.h>
  535. int &max(int &a,int &b)
  536.     {
  537.       if(a>b)
  538.         return a;
  539.        else
  540.           {
  541.             return b;
  542.           }
  543.      }
  544. void main()
  545. { clrscr();
  546. int a,b;
  547. cout<<"enter 2 nos";
  548. cout<<"\n\n";
  549. cin>>a;
  550. cout<<"\n\n";
  551. cin>>b;
  552. max(a,b)=1;
  553. if(a==1)
  554.   {
  555.   cout<<"\n\n";
  556.   cout<<"a is greater";
  557.   }
  558. if(b==1)
  559.   {
  560.    cout<<"\n\n";
  561.    cout<<"b is greater";
  562.   }
  563. getch();
  564. }
  565.  
  566. (18)
  567.  
  568. Program to find Prime number from 1 to 100
  569. #include<iostream.h>
  570. #include<conio.h>
  571. void prime();
  572. void main()
  573. {
  574. clrscr();
  575. prime();
  576. getch();
  577. }
  578. void prime()
  579.  {
  580.   int flag=0;
  581.   cout<<"prime nos. are";
  582.   for(int i=3;i<=100;i++)
  583.      {
  584.         flag=0;
  585.       for(int j=2;j<i;j++)
  586.          {
  587.             if(i%j==0)
  588.               {
  589.                flag=1;
  590.                break;
  591.               }
  592.          }
  593.          if(flag==0)
  594.             cout<<i<<"\t";
  595.       }
  596.  }
  597.  
  598. (19)
  599.  
  600. Program to perform "1.SUM OF DIGITS" & "2.REVERSE OF A NUMBER"
  601. #include<iostream.h>
  602. #include<conio.h>
  603. void sod(int a);
  604. void reverse(int a);
  605. void main()
  606. {
  607. int op,a;
  608. clrscr();
  609. cout<<"1.sum of digit\n\n";
  610. cout<<"2.reverse a no.\n\n";
  611. cin>>op;
  612. switch(op)
  613.  {
  614. case 1: sod(a);
  615.         break;
  616. case 2: reverse(a);
  617.         break;
  618. case 3:cout<<"invalid option";
  619. }
  620. getch();
  621. }
  622. void sod(int a)
  623.  {
  624.   int sum=0,r;
  625.     cout<<"\n\n";
  626.     cout<<"enter a no.";
  627.     cin>>a;
  628.    while(a>0)
  629.       {
  630.     r=a%10;
  631.     a=a/10;
  632.     sum=sum+r;
  633.    }
  634.     cout<<"\n\n";
  635.     cout<<"sum is"<<sum;
  636.  }
  637.  void reverse(int a)
  638.  {
  639.   int b=0,r;
  640.   cout<<"\n\n";
  641.   cout<<"enter a no";
  642.   cin>>a;
  643.   while(a>0)
  644.    {
  645.     r=a%10;
  646.     a=a/10;
  647.     b=b*10+r;
  648.    }
  649.     cout<<"\n\n";
  650.     cout<<"reverse is"<<b;
  651.  }
  652.  
  653. (20)
  654.  
  655. Program to find FACTORIAL using "RECURSION"
  656. #include<iostream.h>
  657. #include<conio.h>
  658. int fact(int n)
  659.  {
  660.   int f=1;
  661.   if(n==1)
  662.     {
  663.      return f;
  664.     }
  665.   else
  666.     {
  667.     f=n*fact(n-1);
  668.     return f;
  669.     }
  670.   }
  671. void main()
  672. {
  673. clrscr();
  674. int n;
  675. cout<<"enter a no.";
  676. cin>>n;
  677. cout<<"\n\n";
  678. cout<<"factorial is"<<fact(n);
  679. getch();
  680. }
  681.  
  682. (21)
  683.  
  684. Program to calculate length of string
  685. #include<stdio.h>
  686. #include<iostream.h>
  687. #include<conio.h>
  688. void count(char a[]);
  689. void main()
  690. {
  691. clrscr();
  692. char a[10],i;
  693. cout<<"\n";
  694. cout<<"enter a string";
  695. cout<<"\n";
  696. gets(a);
  697. count(a);
  698. getch();
  699. }
  700. void count(char a[])
  701.  {
  702.   int countt=0,i;
  703.   for(i=0;a[i]!='\0';i++)
  704.      {
  705.       countt++;
  706.      }
  707.    cout<<"\n";
  708.    cout<<"the length of string is"<<"\t"<<countt;
  709.  }
  710.  
  711. (22)
  712.  
  713. Program to calculate number of VOWELS in a string
  714. #include<stdio.h>
  715. #include<iostream.h>
  716. #include<conio.h>
  717. void vowel(char a[]);
  718. void main()
  719. {
  720. clrscr();
  721. char a[10];
  722. cout<<"enter a string";
  723. gets(a);
  724. vowel(a);
  725. getch();
  726. }
  727. void vowel(char a[])
  728.  {
  729.   int i,count;
  730.   for(i=0;a[i]!='\o';i++)
  731.      {
  732.       if(a[i]=='a'||a[i]=='e'||a[i]=='i'a[i]=='o'||a[i]=='u')
  733.  
  734.          count++;
  735.  
  736.      }
  737.   cout<<"no. of vowels in a string are"<<count;
  738.  }
  739. Email ThisBlogThis!
  740.  
  741. (23)
  742.  
  743. Program to check whether a string is a PALINDROME or not
  744. #include<string.h>
  745. #include<stdio.h>
  746. #include<iostream.h>
  747. #include<conio.h>
  748. void palindrome(char a[]);
  749. void main()
  750. {
  751. clrscr();
  752. char a[10];
  753. cout<<"\n\n";
  754. cout<<"enter a string";
  755. cout<<"\n\n";
  756. gets(a);
  757. cout<<"\n";
  758. palindrome(a);
  759. getch();
  760. }
  761. void palindrome(char a[])
  762.  {
  763.   int i,n,flag=0;
  764.   n=strlen(a);
  765.   for(i=0;i<n/2;i++)
  766.      {
  767.       if(a[i]!=a[n-1-i])
  768.         {
  769.          flag=1;
  770.          break;
  771.         }
  772.       }
  773.   if(flag==0)
  774.     {
  775.      cout<<"\n";
  776.      cout<<"it is a palindrome";
  777.     }
  778.   else
  779.    {
  780.     cout<<"\n";
  781.  
  782.     cout<<"it is not a palindrome";
  783.    }
  784.  }
Advertisement
Add Comment
Please, Sign In to add comment