Language should be Visual Studio C++, and beginner/intermediate, use case, cout,
ID: 3884137 • Letter: L
Question
Language should be Visual Studio C++, and beginner/intermediate, use case, cout,etc.
Write a program to be used as a calculator. It should be able to find addition (+), subtraction (-), multiplication (*), division (/), remainder of integer division (%), exponent(e), and squareroot (s) of numbers. The user enters inputs in the form: operator, number, (number): where the second number is not required for the exponent or squareroot . For example, if the user enters+5 4, then the program prints out 9: or if the user enters s 64, the program prints out 8. Test your program for a wide range of possible inputs including division by zero and squareroot of negative values.Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int num1, num2;
char symbol;
cout<<"Enter the sysmbol and numbers: ";
cin >> symbol;
cin >> num1;
if(symbol != 's' && symbol != 'e') {
cin >> num2;
switch(symbol) {
case '+': printf("Result = %d ", num1+num2);break;
case '-': printf("Result = %d ", num1-num2);break;
case '*': printf("Result = %d ", num1*num2);break;
case '/': printf("Result = %lf ", num1/(double)num2);break;
}
} else{
switch(symbol) {
case 's': printf("Result = %lf ", sqrt(num1));break;
case 'e': printf("Result = %lf ", exp (num1));break;
}
}
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.