fork download
  1. // If you are not sure what some lines of code do, try looking back at
  2. // previous example programs, notes, or ask a question.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.  
  10. // These will all work as you expect
  11. int int1 = 5 + 7;
  12. int int2 = 5 - 7;
  13. int int3 = 5 * 7;
  14.  
  15. // This will evaluate to 0, as 5 / 7 is between 0 and 1
  16. int int4 = 5 / 7;
  17.  
  18. // This will evaluate to 2, as that is the remainder of
  19. // 7 / 5
  20. int mod1 = 7 % 5;
  21.  
  22. // This will evaluate to 5, as 5 does not go into 7 at all,
  23. // so the remainder is just the entire number.
  24. int mod2 = 5 % 7;
  25.  
  26. // This will evaluate to 30, as the multiplication is evaluated
  27. // before the subtraction.
  28. int order1 = 5 * 7 - 5;
  29.  
  30. // This will evaluate to 3, as first the parenthesis is evaluated,
  31. // giving 2, which is then multiplied by 5, giving 10, and finally
  32. // modded with 7, giving a remainder of 3.
  33. int order2 = (7 - 5) * 5 % 7;
  34.  
  35. // After these two lines, the variable will hold the value 12, as it
  36. // starts with 5, and 7 is added to itself.
  37. int combine1 = 5;
  38. combine1 += 7;
  39.  
  40. // After these two lines, the variable will hold 14, as it starts with
  41. // 7, and is multiplied by 7 - 5, or two.
  42. int combine2 = 7;
  43. combine2 *= 7 - 5;
  44.  
  45. // After these two lines, the character will have been transformed to
  46. // a 'g', as it was moved down 5 places.
  47. char char1 = 'b';
  48. char1 += 5;
  49.  
  50. // After these two lines, the character will have been transformed from
  51. // a lowercase e to an uppercase E, as mentioned in the notes.
  52. char char2 = 'e';
  53. char2 = char2 - 'a' + 'A';
  54.  
  55.  
  56.  
  57. // Output results
  58. cout << endl << "Integers:" << endl;
  59. cout << "5 + 7 = " << int1 << endl;
  60. cout << "5 - 7 = " << int2 << endl;
  61. cout << "5 * 7 = " << int3 << endl;
  62. cout << "5 / 7 = " << int4 << endl;
  63.  
  64. cout << endl << "Modulo:" << endl;
  65. cout << "7 % 5 = " << mod1 << endl;
  66. cout << "5 % 7 = " << mod2 << endl;
  67.  
  68. cout << endl << "Order of operations:" << endl;
  69. cout << "5 * 7 - 5 = " << order1 << endl;
  70. cout << "(7 - 5) * 5 % 7 = " << order2 << endl;
  71.  
  72. cout << endl << "Combined assignment:" << endl;
  73. cout << "5 += 7 = " << combine1 << endl;
  74. cout << "5 *= 7 - 5 = " << combine2 << endl;
  75.  
  76. cout << endl << "Character math:" << endl;
  77. cout << "b + 5 = " << char1 << endl;
  78. cout << "e - a + A = " << char2 << endl;
  79.  
  80. // Hold the screen for the user
  81. cout << endl;
  82. system("pause");
  83.  
  84. return 0;
  85. }
  86.  
Success #stdin #stdout #stderr 0s 3460KB
stdin
Standard input is empty
stdout
Integers:
5 + 7 = 12
5 - 7 = -2
5 * 7 = 35
5 / 7 = 0

Modulo:
7 % 5 = 2
5 % 7 = 5

Order of operations:
5 * 7 - 5 = 30
(7 - 5) * 5 % 7 = 3

Combined assignment:
5 += 7 = 12
5 *= 7 - 5 = 14

Character math:
b + 5 = g
e - a + A = E

stderr
sh: 1: pause: not found