Answer:
This question is answered using C++
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
int ope, yourresult;
cout<<"Select Operator: \n"<<"1 for addition\n"<<"2 for subtraction\n"<<"3 for multiplication\n";
cout<<"Operator: ";
cin>>ope;
srand((unsigned) time(0));
int num1 = rand() % 12;
int num2 = rand() % 12;
int result = 1;
if(ope == 1){
cout<<num1<<" + "<<num2<<" = ";
result = num1 + num2;
}
else if(ope == 2){
cout<<num1<<" - "<<num2<<" = ";
result = num1 - num2;
}
else if(ope == 3){
cout<<num1<<" * "<<num2<<" = ";
result = num1 * num2;
}
else{
cout<<"Invalid Operator";
}
cin>>yourresult;
if(yourresult == result){
cout<<"Correct!";
}
else{
cout<<"Incorrect!";
}
return 0;
}
Explanation:
This line declares operator (ope) and user result (yourresult) as integer
int ope, yourresult;
This prints the menu
cout<<"Select Operator: \n"<<"1 for addition\n"<<"2 for subtraction\n"<<"3 for multiplication\n";
This prompts the user for operator
cout<<"Operator: ";
This gets user input for operator
cin>>ope;
This lets the program generates different random numbers
srand((unsigned) time(0));
This generates the first random number
int num1 = rand() % 12;
This generates the second random number
int num2 = rand() % 12;
This initializes result to 1
int result = 1;
If the operator selected is 1 (i.e. addition), this prints an addition operation and calculates the actual result
<em> if(ope == 1){</em>
<em> cout<<num1<<" + "<<num2<<" = ";</em>
<em> result = num1 + num2;</em>
<em> }</em>
If the operator selected is 2 (i.e. subtraction), this prints an subtracttion operation and calculates the actual result
<em> else if(ope == 2){</em>
<em> cout<<num1<<" - "<<num2<<" = ";</em>
<em> result = num1 - num2;</em>
<em> }</em>
If the operator selected is 3 (i.e. multiplication), this prints an multiplication operation and calculates the actual result
<em> else if(ope == 3){</em>
<em> cout<<num1<<" * "<<num2<<" = ";</em>
<em> result = num1 * num2;</em>
<em> }</em>
If selected operator is not 1, 2 or 3, the program prints an invalid operator selector
<em> else{</em>
<em> cout<<"Invalid Operator";</em>
<em> }</em>
This gets user input
cin>>yourresult;
This checks if user result is correct and prints "Correct!"
if(yourresult == result){
cout<<"Correct!";
}
If otherwise, the program prints "Incorrect!"
<em> else{</em>
<em> cout<<"Incorrect!";</em>
<em> }</em>
return 0;