232k views
3 votes
Create a program that displays a menu to select addition, subtraction, or multiplication. Using random numbers between 0 and 12 (including 0 and 12), present a math problem of the type selected to the user. Display a "correct" or "incorrect" message after the user enters their answer. In C++

1 Answer

4 votes

Answer:

This question is answered using C++

#include<iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main(){

int ope, yourresult;

cout<<"Select Operator: \\"<<"1 for addition\\"<<"2 for subtraction\\"<<"3 for multiplication\\";

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;

}

Step-by-step explanation:

This line declares operator (ope) and user result (yourresult) as integer

int ope, yourresult;

This prints the menu

cout<<"Select Operator: \\"<<"1 for addition\\"<<"2 for subtraction\\"<<"3 for multiplication\\";

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

if(ope == 1){

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

result = num1 + num2;

}

If the operator selected is 2 (i.e. subtraction), this prints an subtracttion operation and calculates the actual result

else if(ope == 2){

cout<<num1<<" - "<<num2<<" = ";

result = num1 - num2;

}

If the operator selected is 3 (i.e. multiplication), this prints an multiplication operation and calculates the actual result

else if(ope == 3){

cout<<num1<<" * "<<num2<<" = ";

result = num1 * num2;

}

If selected operator is not 1, 2 or 3, the program prints an invalid operator selector

else{

cout<<"Invalid Operator";

}

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!"

else{

cout<<"Incorrect!";

}

return 0;

User Adean
by
4.8k points