fork download
  1. // Nicolas Ruano CS1A Chapter 3, Pp. 143, #15
  2. /*******************************************************************************
  3.  * CALCULATING A BASIC MATHEMATICAL PROBLEM
  4.  * ____________________________________________________________________________
  5.  * This program that can be used as a math tutor for a young student.The
  6.  * program should display two random numbers:
  7.  * 247 + 129 =
  8.  ******************************************************************************/
  9.  
  10. #include <iostream>
  11. #include <cstdlib> // For rand() and srand()
  12. #include <ctime> // For time()
  13. using namespace std;
  14.  
  15. int main() {
  16. // Seed the random number generator
  17. srand(time(0));
  18.  
  19. // Generate two random numbers (3-digit range like the example)
  20. int num1 = rand() % 900 + 100; // from 100 to 999
  21. int num2 = rand() % 900 + 100; // from 100 to 999
  22.  
  23. // Display the problem
  24. cout << "Solve this problem:" << endl;
  25. cout << " " << num1 << endl;
  26. cout << "+ " << num2 << endl;
  27. cout << "-----" << endl;
  28.  
  29. // Ask for student's answer
  30. int answer;
  31. cout << "Your answer: ";
  32. cin >> answer;
  33.  
  34. // Show correct answer
  35. cout << "Correct answer: " << (num1 + num2) << endl;
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Solve this problem:
  770
+ 664
-----
Your answer: Correct answer: 1434