Write a C++ program that promotes the user to enter 2 double values, then ask the user to enter a mathematical sign either ‘+' or ‘-‘ or ‘x' or ‘/'. Based on the input sign, the program should perform the corresponding mathematical operation and print the result.

Respuesta :

Answer:

#include<iostream>

using namespace std;

int main(){

double num1, num2;

char ope;

cout<<"Enter two numbers: ";

cin>>num1>>num2;

cout<<"Enter an operator +, -, /, * :";

cin>>ope;

if(ope == '+'){

cout<<num1+num2;}

else if(ope == '-'){

cout<<num1-num2;}

else if(ope == '*'){

cout<<num1*num2;}

else if(ope == '/'){

cout<<num1/num2;}

else{

cout<<"Invalid operator";}

return 0;

}

Explanation:

#include<iostream>

using namespace std;

int main(){

This line declares num1 and num2 as double

double num1, num2;

This line declares ope as a character

char ope;

This line prompts user for user inputs

cout<<"Enter two numbers: ";

This line gets user inputs

cin>>num1>>num2;

This line prompts user for operator

cout<<"Enter an operator +, -, /, * :";

This line gets the operator

cin>>ope;

This condition checks if the operator is +. If yes, it adds and prints the result

if(ope == '+'){

cout<<num1+num2;}

This condition checks if the operator is -. If yes, it subtracts and prints the result

else if(ope == '-'){

cout<<num1-num2;}

This condition checks if the operator is *. If yes, it multiplies and prints the result

else if(ope == '*'){

cout<<num1*num2;}

This condition checks if the operator is /. If yes, it divides and prints the result

else if(ope == '/'){

cout<<num1/num2;}

This checks for invalid operators

else{

cout<<"Invalid operator";}

return 0;

}

ACCESS MORE