(I'm very confused with this, kinda not understanding, any chance you can help answer and explain. This is for devc++, i use version 5.11) Implement the Calculator code we covered on Oct 13th using switch
(I'm very confused with this, kinda not understanding, any chance you can help answer and explain. This is for devc++, i use version 5.11)
Implement the Calculator code we covered on Oct 13th using switch
/* 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";
}
Step by step
Solved in 3 steps with 1 images