fork download
  1. // Nicolas Ruano CS1A Chapter 3, Pp. 143, #15
  2. /*******************************************************************************
  3.  * CALCULATING A BASIC MATHEMATICAL PROBLEM
  4.  * ____________________________________________________________________________
  5.  * This program demeostrates of how you can write a basic mathematical problem
  6.  * in C++, with teh following steps to solve
  7.  * ____________________________________________________________________________
  8.  * Given values to solve
  9.  * 247
  10.  * 129
  11.  * Add all the values to get the final result for the program to execute
  12.  ******************************************************************************/
  13. #include <iostream>
  14. #include <cstdlib> // For rand() and srand()
  15. #include <ctime> // For time()
  16. using namespace std;
  17.  
  18. int main() {
  19. // Seed random number generator
  20. srand(time(0));
  21.  
  22. // Generate two random numbers between 100 and 999
  23. int num1 = rand() % 900 + 100;
  24. int num2 = rand() % 900 + 100;
  25.  
  26. // Display the problem
  27. cout << "Solve this problem:" << endl;
  28. cout << " " << num1 << endl;
  29. cout << "+ " << num2 << endl;
  30. cout << "-----" << endl;
  31.  
  32. // Pause for student input (even if not used)
  33. int answer;
  34. cout << "Your answer: ";
  35. cin >> answer;
  36.  
  37. // Show the correct answer
  38. cout << "Correct answer: " << (num1 + num2) << endl;
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
Solve this problem:
  384
+ 149
-----
Your answer: Correct answer: 533