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.  * -----
  12.  * Add all the values to get the final result for the program to execute
  13.  ******************************************************************************/
  14. #include <iostream>
  15. #include <cstdlib> // For rand() and srand()
  16. #include <ctime> // For time()
  17. using namespace std;
  18.  
  19. int main() {
  20. // Seed random number generator
  21. srand(time(0));
  22.  
  23. // Generate two random numbers between 100 and 999
  24. int num1 = rand() % 900 + 100;
  25. int num2 = rand() % 900 + 100;
  26.  
  27. // Display the problem
  28. cout << "Solve this problem:" << endl;
  29. cout << "247" << num1 << endl;
  30. cout << "+129" << num2 << endl;
  31. cout << "-----" << endl;
  32.  
  33. // Pause for student input (even if not used)
  34. int answer;
  35. cout << "Your answer: ";
  36. cin >> answer;
  37.  
  38. // Show the correct answer
  39. cout << "Correct answer: " << (num1 + num2) << endl;
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Solve this problem:
247679
+129953
-----
Your answer: Correct answer: 1632