Advertisement
The_Newt

[Guide & Examples] C++

Oct 6th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | None | 0 0
  1. // coded by newt
  2. // not compatible with programs running c++ yet; for educational purposes only
  3. // all codes below (except // codes) are original and thought of in the brightest way possible
  4. // they are all examples, so feel free to modify them in any manner if possible
  5.  
  6.  
  7. // syntax of ternary operators:
  8. (condition) ? (if_true) : (if_false)
  9.  
  10. // example of a ternary operator:
  11. int x=15;
  12. int y=(x==15)? 10:20; //here it means if x is equal to 15 then y will be assigned 10 otherwise 20
  13. cout<<(x<=10)?"Good";"Not so good";
  14.  
  15. // syntax of the IF, ELSE construct:
  16. if(condition)
  17. {
  18.     statement 1;
  19. }
  20. else
  21. {
  22.     statement 2;
  23. }
  24.  
  25. // example of the IF, ELSE construct:
  26. if(a>b)
  27. {
  28.     cout<<"A is greater than B";
  29. }
  30. else
  31. {
  32.     cout<<"B is greater than A";
  33. }
  34.  
  35. // first example of the nested IF, ELSE/IF, ELSE ladder:
  36. if(condition1)
  37. {
  38.     statement 1;
  39. }
  40. else if(condition2)
  41. {
  42.     statement 2;
  43. }
  44. else
  45. {
  46.     statement 3;
  47. }
  48.  
  49. // second example of the nested IF, ELSE/IF, ELSE ladder:
  50. if(condition1)
  51. if(condition2)
  52. .
  53. .
  54. .
  55. if(condition3)
  56. {
  57.     statement 1;
  58. }
  59. else
  60. {
  61.     statement 2;
  62. .
  63. .
  64. .
  65. }
  66. else
  67. {
  68.     statement 3;
  69. }
  70. else
  71. {
  72.     statement 4;
  73. }
  74.  
  75. // syntax of the SWITCH, CASE construct:
  76. switch(switch variable)
  77. {
  78.     case <constant1>: statement1;
  79.         .
  80.         .
  81.         .
  82.         statement n;
  83.         break;
  84.         .
  85.         .
  86.         .
  87.     case <constantn>: statement1;
  88.         .
  89.         .
  90.         .
  91.         statement n;
  92.         break;
  93.         .
  94.         .
  95.         .
  96.         default : statement1;
  97.         .
  98.         .
  99.         .
  100.         statement n;
  101. }
  102.  
  103. // example of the default case option in the SWITCH, CASE construct:
  104. switch(choice)
  105. {
  106.     case 1: cout<<"Sum: ";
  107.         cout<<a+b<<end1;
  108.         break;
  109.     case 2: cout<<"Difference: ";
  110.         cout<<a-b<<end1;
  111.         break;
  112.     case 3: cout<<"Product: ";
  113.         cout<<a*b<<end1;
  114.         break;
  115.     case 4: cout<<"Quotient: ";
  116.         cout<<a/b<<end1;
  117.         break;
  118.     case 5: cout<<"Remainder: ";
  119.         cout<<a%b<<end1;
  120.         break;
  121.     default: cout<<"Invalid choice!";
  122. }
  123.  
  124. // to accept a number from the user and check if it is an odd or even number:
  125. #include<iostream.h>
  126. #include<conio.h>
  127. void main()
  128. {
  129.     clrscr();
  130.     int x;
  131.     cout<<"Enter a number";
  132.     cin>>x;
  133.     if(x%2==0)  // checks if the remainder is 0
  134.     cout<<"The number you entered is an even number";
  135.     else
  136.     cout<<"The number you entered is an odd number";
  137. }
  138.  
  139. // to accept and alphanumeric value from the user and check if it is in upper case or lower case:
  140. #include<iostream.h>
  141. #include<conio.h>
  142. void main()
  143. {
  144.     clrscr();
  145.     char alpha;
  146.     cout<<"Enter an alphabet";
  147.     cin>>alpha;
  148.     if(alpha>='a' && alpha<='z')
  149.     cout<<"The alphabet you entered is in lower case";
  150.     else if(alpha>='A' && alpha<='Z')
  151.     cout<<"The alphabet you entered is in upper case";
  152. }
  153.  
  154. // to accept marks of a student in 5 subjects:
  155. #include<iostream.h>
  156. #include<conio.h>
  157. void main()
  158. {
  159.     int m1,m2,m3,m4,m5,per;
  160.     char grade;
  161.     cout<<"Enter your marks in any five subjects: "<<end1;
  162.     cin>>m1>>m2>>m3>>m4>>m5;
  163.     per = (m1+m2+m3+m4+m5)/500 * 100;
  164.     if(per>=90)
  165.         grade='A';
  166.     else if(per>=80)
  167.         grade='B';
  168.     else if(per>=70)
  169.         grade='C';
  170.     else if(per>=60)
  171.         grade='D';
  172.     else if(per>=50)
  173.         grade='E';
  174.     else
  175.         grade='F';
  176.     cout<<"Your grade is: "<<grade;
  177. }
  178.  
  179. // to accept a number from the user and check if it is a big, medium or small number:
  180. #include<iostream.h>
  181. #include<conio.h>
  182. void main()
  183. {
  184.     clrscr;
  185.     int num;
  186.     cout<<"Enter a number between 0 and 100";
  187.     cin>>num;
  188.     if num>>100;
  189.     cout<<"The number you entered is a big number";
  190.     else if num>>90;
  191.     cout<<"The number you entered is a big number";
  192.     else if num>>80;
  193.     cout<<"The number you entered is a big number";
  194.     else if num>>70;
  195.     cout<<"The number you entered is a medium number";
  196.     else if num>>60;
  197.     cout<<"The number you entered is a medium number";
  198.     else if num>>50;
  199.     cout<<"The number you entered is a medium number";
  200.     else if num>>40;
  201.     cout<<"The number you entered is a medium number";
  202.     else if num>>30;
  203.     cout<<"The number you entered is a medium number";
  204.     else if num>>20;
  205.     cout<<"The number you entered is a small number";
  206.     else if num>>10;
  207.     cout<<"The number you entered is a small number";
  208.     else if num>>0;
  209.     cout<<"The number you entered is zero";
  210.     else
  211.     cout<<"Incorrect number! Enter a number divisible by 10";
  212. }
  213.  
  214. // more c++ codes coming soon!
  215.  
  216.  
  217. // congratulations! you have finished newt's c++ example guide!
  218. // if you read through everything and used some of it - GREAT!
  219. // if you just scrolled to the end - LOSER!
  220.  
  221. // copyright 2014 newt drost
  222. // distributed by the newt productions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement