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
- Add two header file for random number.
- Declare two int variables for random numbers.
- Declare another a int variable for calculation.
- Declare a variable for time.
- Use srand and rand keyword for random number.
- Display random number on the screen.
- Calculate answer.
- Use cin.get for press enter ( for see solution ).
- In last, display output on the screen in a pattern.
- Return 0 to the main function.
0 Comments