Write a program that reads in an operator and two operands, performs the operati
ID: 643008 • Letter: W
Question
Write a program that reads in an operator and two operands, performs the operation, and then prints out the result. Input: The input will be one or more lines of numbers that have been typed in by the user. Each line is to have an operator followed by two operands. The operator can be one of the seven: +, -, *, /, %, power, and average. Output: The output will have for each line, the first operand followed by a space followed by the operator followed by a space followed by the second operand followed by a space followed by = followed by a space followed by the result. Each number is to be printed using "%.2e" format. Use in.next() to read the operator and in.nextDouble() to read a number. Use String class's equals method to compare two strings.
Explanation / Answer
# include <stdio.h>
int main()
{
char o;
float num1,num2;
printf("Select an operator either + or - or * or / ");
scanf("%c",&o);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(o)
{
case '+': printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case '-': printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case '*': printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case '/': printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
default: /* If operator is other than +, -, * or /, error message is shown */ printf("Error! operator is not correct");
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.