Implement the Calculator code, this's C++
/* Implement a
calculator
take the input for a character
if character is "+", then it's going to take the input of two
numbers and show the sum
if character is "-", then it's going to take the input of two
numbers and show the minus result
if character is "*", then it's going to take the input of two
numbers and show the product
if character is "/", then it's going to take the input of two
numbers and show the divide result
if character is "%", then it's going to take the input of two
numbers and show the modulus result
if character is "^", then it's going to take the input of two
numbers and show the number1^number2
*/
#include
<iostream>
#include <cmath>
using namespace std;
int main(){
char operation;
int number1, number2;
cout << "Enter +, -, *, /, %, ^";
cin >> operation;
if ( operation == '+' ){
//take the input of two numbers and display the sum
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Sum is = "<<
(number1+number2)<<endl;
}
else if ( operation == '-' ){
//take the input of two numbers and display the minus result
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Number 2 subtracted from number 1 is = "<<
(number1-number2)<<endl;
}
else if ( operation == '*' ){
//take the input of two numbers and display the sum
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Product is = "<<
(number1*number2)<<endl;
}
else if ( operation == '/' ){
//take the input of two numbers and display the divide result
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Number1 divided by number 2 is = "<<
(number1/number2)<<endl;
}
else if ( operation == '%' ){
//take the input of two numbers and display the modulus
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Modulus is = "<<
(number1%number2)<<endl;
}
else if ( operation == '^' ){
//take the input of two numbers and display the pow
cout << "Enter two numbers";
cin >> number1 >> number2;
cout << "Number1 raised to number2 is = "<<
(pow(number1,number2))<<endl;
}
else
cout << "Invalid operator";
}
*******************Summary********************
The program is given below with changes and comments and output
Now the program asks repeatedly for the next operation......with exiting message..And i have done some changes to tje existing code to make it easier...
The explanation for changes is provided in comments..
I hope it works for you :) !!!!!!!!!!!
*******************Program***********************
#include <iostream>
#include <cmath> //required forusing pow() function
using namespace std;
int main()
{
char operation; //varable for storing type of operation
int number1, number2; //variables for storinf numbers on which
// operation is going to be performed
while(1) //loop for asking for operation again
{
cout << "\nEnter +, -, *, /, %, ^ (0 for exit) : ";
cin >> operation; //take input for type of operation
switch(operation) //match operation value with cases
//and execute respective case
{
case '+': //if operation is + then do follwing
//take the input of two numbers and display the sum
cout << "Enter two numbers\n";
cin >> number1 >> number2;
cout << "Sum is = "<< (number1+number2)<<endl;
break; //break out of switch case
case '-': //if operation is -
//take the input of two numbers and display the minus result
cout << "Enter two numbers \n";
cin >> number1 >> number2;
cout << "Number 2 subtracted from number 1 is = "<< (number1-number2)<<endl;
break; //break out of switch case
case '*': //if operation is *
//take the input of two numbers and display the sum
cout << "Enter two numbers \n";
cin >> number1 >> number2;
cout << "Product is = "<< (number1*number2)<<endl;
break; //break out of switch case
case '/': //if operation is /
//take the input of two numbers and display the divide result
cout << "Enter two numbers \n";
cin >> number1 >> number2;
cout << "Number1 divided by number 2 is = "<< (number1/number2)<<endl;
break; //break out of switch case
case '%': //if operation is %
//take the input of two numbers and display the modulus
cout << "Enter two numbers \n";
cin >> number1 >> number2;
cout << "Modulus is = "<< (number1%number2)<<endl;
break; //break out of switch case
case '^': //if operation is ^
//take the input of two numbers and display the pow
cout << "Enter two numbers\n";
cin >> number1 >> number2;
cout << "Number1 raised to number2 is = "<< (pow(number1,number2))<<endl;
break; //break out of switch case
case '0':
cout<<"Goodbye :)"; //if user wants to exit
exit(0); //exit the program with return 0
default: //if operation does not match any cases then
cout << "Invalid operator";
break; //break out of switch case
}//end of switch case
}//end of while loop
}
*******************Output*************************
Get Answers For Free
Most questions answered within 1 hours.