fork download
  1. //Amari Mosley CSC5 Chapter 3, P. 143, #15
  2. //
  3. /**************************************************************
  4.  *
  5.  * Math Tutor
  6.  * ____________________________________________________________
  7.  * This program can be used as a math tutor for a young student.
  8.  *
  9. The formula for sum is as follows:
  10. Sum = num1 + num2
  11.  * ____________________________________________________________
  12.  * INPUT
  13.  * User pressing 'Enter' to reveal the answer
  14.  * OUTPUT
  15.  * Two random numbers and their sum
  16.  *
  17.  **************************************************************/
  18. #include <iostream>
  19. #include <iomanip>
  20. #include <cstdlib>
  21. #include <ctime>
  22. using namespace std;
  23.  
  24. // Defining Main Function
  25. int main() {
  26. // Define variables to hold the two random numbers and the sum
  27. int num1, num2, sum;
  28.  
  29. // Get the system time to use as the seed for the random number generator
  30. unsigned seed = time(0);
  31.  
  32. // Seed the random number generator
  33. srand(seed);
  34.  
  35. // Generate two random 3-digit numbers (between 100 and 999)
  36. num1 = (rand() % 900) + 100;
  37. num2 = (rand() % 900) + 100;
  38.  
  39. // Calculate the sum
  40. sum = num1 + num2;
  41.  
  42. // Display the problem formatted as requested[cite: 5]
  43. cout << setw(5) << num1 << endl;
  44. cout << "+" << setw(4) << num2 << endl;
  45. cout << "-----" << endl;
  46.  
  47. // Pause the program while the student works on the problem[cite: 5]
  48. cout << "\nPress Enter when you are ready to check your answer...";
  49. cin.get();
  50.  
  51. // Display the problem again along with the correct solution[cite: 5]
  52. cout << "\n";
  53. cout << setw(5) << num1 << endl;
  54. cout << "+" << setw(4) << num2 << endl;
  55. cout << "-----" << endl;
  56. cout << setw(5) << sum << endl;
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
  251
+ 555
-----

Press Enter when you are ready to check your answer...
  251
+ 555
-----
  806