fork(1) download
  1. #include <iostream>
  2. #include <cstdlib> // For rand() and srand()
  3. #include <ctime> // For time()
  4. using namespace std;
  5.  
  6. int main() {
  7. // Seed random number generator
  8. srand(static_cast<unsigned int>(time(0)));
  9.  
  10. // Generate two random numbers between 100 and 999
  11. int num1 = rand() % 900 + 100; // 100 to 999
  12. int num2 = rand() % 900 + 100; // 100 to 999
  13.  
  14. // Display the addition problem
  15. cout << "Solve the following problem:\n\n";
  16. cout << " " << num1 << endl;
  17. cout << "+ " << num2 << endl;
  18. cout << "-----\n";
  19.  
  20. // Pause for the student to think
  21. cout << "\nPress Enter when you are ready to see the answer...";
  22. cin.ignore(); // Ignore leftover newline from previous input
  23. cin.get(); // Wait for Enter key
  24.  
  25. // Display the correct solution
  26. int sum = num1 + num2;
  27. cout << "\nCorrect solution: 376\n";
  28. cout << " " << num1 << endl;
  29. cout << "+ " << num2 << endl;
  30. cout << "-----\n";
  31. cout << " " << sum << endl;
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Solve the following problem:

  703
+ 887
-----

Press Enter when you are ready to see the answer...
Correct solution: 376
  703
+ 887
-----
  1590