100k views
3 votes
Create a script to input 2 numbers from the user. The script will then ask the user to perform a numerical calculation of addition, subtraction, multiplication, or division. Once the calculation is performed, the script will end.

User Dheinz
by
7.8k points

1 Answer

3 votes

Answer:

The code given is written in C++

First we declare the variables to be used during the execution. The names given are self-explanatory.

Then the program outputs a request on the screen and waits for user input, for both numbers and one more time for the math operation wanted, selected with numbers 1 to 4.

Finally, the program executes the operation selected and outputs the result on screen.

Code:

#include <iostream>

int main()

{

// variable declaration

float numberA;

float numberB;

int operation;

float result=0;

//number request

std::cout<<"Type first number:\\"; std::cin>>numberA;

std::cout<<"Type second number:\\"; std::cin>>numberB;

//Operation selection

cout << "Select an operation\\";

cout << "(1) Addition\\";

cout << "(2) Subtraction\\";

cout << "(3) Multiplication\\";

cout << "(4) Division\\";

std::cout<<"Operation:\\"; std::cin>>operation;

switch(operation){

case 1:

result = numberA+numberB;

break;

case 2:

result = numberA-numberB;

break;

case 3:

result = numberA*numberB;

break;

case 4:

result = numberA/numberB;

break;

default:

std::cout<<"Incorrect option\\";

}

//Show result

std::cout<<"Result is:"<<result<<::std::endl;

return 0;

}

User Geeky I
by
7.3k points