fork download
  1. // Torrez, Elaine CS1A Chapter 3 P. 146, #15
  2.  
  3. /******************************************************************************************
  4.  *
  5.  * CREATE MATH TUTOR PROBLEM
  6.  *
  7.  * --------------------------------------------------------------------------------
  8.  * This program generates two random numbers and displays them as an addition
  9.  * problem for the student to solve. The student presses Enter to see the solution.
  10.  * --------------------------------------------------------------------------------
  11.  *
  12.  * INPUT
  13.  * (Press Enter key to check answer)
  14.  *
  15.  * OUTPUT
  16.  * num1 : First random number
  17.  * num2 : Second random number
  18.  * total : Correct sum (num1 + num2)
  19.  *
  20.  *******************************************************************************************/
  21.  
  22. #include <iostream>
  23. #include <cstdlib> // For rand() and srand()
  24. #include <ctime> // For time() to seed random generator
  25. using namespace std;
  26.  
  27. int main ()
  28. {
  29. int num1; // First random number
  30. int num2; // Second random number
  31. int total; // Correct sum
  32.  
  33. // Seed random number generator
  34. srand(time(0));
  35.  
  36. // Generate two random numbers between 100 and 999
  37. num1 = rand() % 900 + 100;
  38. num2 = rand() % 900 + 100;
  39.  
  40. // Calculate total
  41. total = num1 + num2;
  42.  
  43. // Display the problem
  44. cout << " " << num1 << endl;
  45. cout << "+ " << num2 << endl;
  46. cout << "------" << endl;
  47.  
  48. // Pause until student presses Enter
  49. cin.get();
  50.  
  51. // Show the answer
  52. cout << " " << total << endl;
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
  837
+ 448
------
  1285