fork download
  1. //Jeremy Huang CS1A Chapter 3, P. 146, #15
  2. //
  3. /**************************************************************
  4.  *
  5.  * CALCULATE ADDITION PROBLEM
  6.  * ____________________________________________________________
  7.  * This program displays an addition problem and pauses until
  8.  * it receives an input to display the answer
  9.  * ____________________________________________________________
  10.  * INPUT
  11.  * N/A : Program waits for Enter key to be pressed
  12.  *
  13.  * OUTPUT
  14.  * num1 : first random number
  15.  * num2 : second random number
  16.  * sum : sum of num1 and num2
  17.  *
  18.  **************************************************************/
  19.  
  20. #include <iostream>
  21. #include <cstdlib>
  22. #include <ctime>
  23. #include <iomanip>
  24. using namespace std;
  25.  
  26. int main() {
  27. srand(time(0));
  28. //Random numbers from 1-500
  29. int num1 = rand() % 401 + 100;
  30. int num2 = rand() % 401 + 100;
  31.  
  32. //Calculate answer
  33. int sum = num1 + num2;
  34.  
  35. //Display addition problem
  36. cout<<"Here is a math problem to solve:\n\n";
  37. cout<<setw(5)<<num1<<endl;
  38. cout<<"+ "<<setw(3)<<num2<<endl;
  39. cout<<"-----\n"<<endl;
  40.  
  41. //Prompt user to press Enter when ready
  42. cout<<"Press the Enter key when you are ready to see the answer";
  43. cin.get();
  44.  
  45. //Display solution
  46. cout<<"\nHere is the correct answer:\n\n";
  47. cout<<setw(5)<<num1<<endl;
  48. cout<<"+ "<<setw(3)<<num2<<endl;
  49. cout<<"-----\n";
  50. cout<<setw(5)<<sum<<endl;
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Here is a math problem to solve:

  291
+ 359
-----

Press the Enter key when you are ready to see the answer
Here is the correct answer:

  291
+ 359
-----
  650