0% found this document useful (0 votes)
18 views1 page

Calculator

Prompt the user to enter two numbers. Prompt the user to select an arithmetic operation (addition, subtraction, multiplication, division). Use a switch statement to perform the selected operation. Display the result of the operation. Ensure the program handles invalid operations and division by zero gracefully and handles the positive and negative values.

Uploaded by

yasserking291
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

Calculator

Prompt the user to enter two numbers. Prompt the user to select an arithmetic operation (addition, subtraction, multiplication, division). Use a switch statement to perform the selected operation. Display the result of the operation. Ensure the program handles invalid operations and division by zero gracefully and handles the positive and negative values.

Uploaded by

yasserking291
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

using namespace std;

int main()
{
float x,y,r;
char operation;
cout << "Enter the First Number: " << endl;
cin>>x;
cout << "Enter the Second Number: " << endl;
cin>>y;
cout << "Select the Type of Operation: " << endl;
cout << "1. Addition : (+)"<< endl;
cout << "2. Subtraction : (-)"<< endl;
cout << "3. Multiplication : (*)"<< endl;
cout << "4. Division : (/)"<< endl;
cout << "Enter your choice (+,-,*,/): "<< endl;
cin>>operation;
switch (operation){
case '+':
r=x+y;
cout << "Result: " <<x<< " + " <<y<< " = " <<r<< endl;
break;
case '-':
r=x-y;
cout << "Result: " <<x<< " - " <<y<< " = " <<r<< endl;
break;
case '*':
r=x*y;
cout << "Result: " <<x<< " * " <<y<< " = " <<r<< endl;
break;
case'/':
if(y!=0){
r=x/y;
cout << "Result: " <<x<< " / " <<y<< " = " <<r<< endl;
}
else{
cout << "Error: Division by zero is not allowed ,Please Try again." << endl;
}
break;
default:
cout << "Error: Invalid operation selected." << endl;
break;
}
return 0;
}

You might also like