Write a C++ program that can be used as a math tutor for a young student. The pr
ID: 3666024 • Letter: W
Question
Write a C++ program that can be used as a math tutor for a young student. The program should display two random numbers to be added or subtracted, such as
247
+ 129
------------
or
56
- 867
-----------
The program should choose randomly whether the problem is addition or subtraction. The program should wait for the student to enter the answer. If the answer is correct, a message of congratulations should be printed. If the answer is incorrect, a message should be printed showing the correct answer. Make sure the numeric terms are right-aligned, even if they have different numbers of digits
Explanation / Answer
#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
// generating two numbers in integer range
srand(time(NULL));
int num1 = rand()/32767;
int num2 = rand()/32767;
cout<<"First Number : "<<num1<<endl;
cout<<"Second Number : "<<num2<<endl;
cout<<"Enter your result: ";
int userResult;
cin>>userResult;
//generating 0 or 1 randomly-- 0 means subtraction, 1 means addition
srand( time(NULL) );
int randNum = rand() % 2; // Generate a random number between 0 and 1
int myResult;
if(randNum == 0)
myResult = num1 + num2;
else
myResult = num1 + num2;
if(myResult == userResult)
cout<<"congratulation!!!";
else
cout<<"Incorrect gusse, correct answer is: "<<myResult<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.