fork download
  1. //Adrian Lazaro CSC5 Chapter 4, P. 221, #9
  2. //
  3. /*****************************************************************************
  4.  *
  5.  * COMPUTE TWO RANDOM NUMBERS
  6.  * __________________________________________________________________________
  7.  * This program gives the user a random math problem to solve. if solved
  8.  * correctly user will be congratulated if wrong program shows right answer.
  9.  *
  10.  * Computation is based on formula:
  11.  * outputAnswer = num1 + num2
  12.  * __________________________________________________________________________
  13.  * INPUT
  14.  * num1 : First random number
  15.  * num2 : Second random number
  16.  *
  17.  * OUTPUT
  18.  * Depending on random numbers
  19.  *
  20.  ****************************************************************************/
  21. #include <iostream>
  22. #include <cstdlib>
  23. #include <ctime>
  24. using namespace std;
  25. int main()
  26. {
  27. unsigned seed = time(0);
  28. //Declare variables
  29. int num1;
  30. int num2;
  31. int inputAnswer;
  32. int outputAnswer;
  33.  
  34. srand(seed); //Creates random number
  35.  
  36. num1 = 1 + rand() % 999; //Assigns random number 1-999 to num1
  37. num2 = 1 + rand() % 999; //Assigns randim number 1-999 to num2
  38. outputAnswer = num1 + num2; //CALCULATION
  39. //Display's math problem
  40. cout << num1 << endl;
  41. cout << num2 << endl;
  42. cin >> inputAnswer; //User answer to problem
  43. //OUTPUT
  44. if (inputAnswer == outputAnswer)
  45. {
  46. cout << "\nCongratulations you got the right answer!";
  47. }
  48. else
  49. {
  50. cout << "\nWrong answer, the correct answer is " << outputAnswer;
  51. }
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 5444KB
stdin
1144
stdout
479
665

Congratulations you got the right answer!