Programming Challenge Chapter 3 – Q #15 Math Tutor – Tony Gaddis – Starting Out With C++

Programming Challenge Chapter 3 – Q #15  Math Tutor – Tony Gaddis – Starting Out With C++



Problem: -

Write a program that can be used as a math tutor for a young student. The program should display two random numbers to be added, such as
                     247 + 129 = 
The program should then pause while the student works on the problem. When the student is ready to check the answer, he or she can press a key and the program will display the correct solution: 
                    247 + 129 = 376

Solution: -

#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

int main()

{

      int num1, num2;

      int ans;

      unsigned seed = time(0);

      srand(seed);

      num1 = (rand() % 900 + 1) + 100;

      num2 = (rand() % 900 + 1) + 100;

      cout << num1 << " + " << num2 << endl;

      ans = num1 + num2;

      cout << "Press Enter To See Solution.";

      cin.get();

      cout << num1 << " + " << num2 << " = " << ans << endl;

      return 0;

}

This is the solution of this question




OUTPUT OF THIS QUESTION 

Input is highlighted with yellow color.


Explanation of this Solution

  1. Add two header file for random number.
  2. Declare two int variables for random numbers.
  3. Declare another a int variable for calculation.
  4. Declare a variable for time.
  5. Use srand and rand keyword for random number.
  6. Display random number on the screen.
  7. Calculate answer.
  8. Use cin.get for press enter ( for see solution ).
  9.  In last, display output on the screen in a pattern.
  10. Return 0 to the main function.

Also, I attach CPP file of this problem. You can download this file

Click Here to Download This File


Post a Comment

0 Comments