Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program, which will act as a simple four-function calculator. That is it

ID: 3564145 • Letter: W

Question

Write a program, which will act as a simple four-function calculator. That is it will read a number, read an operator, read another number, then do the operation. The calculator works with integers and uses four functions: +, -, *, and /. After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation. If the user enters a C the result is cleared and then the user starts entering a new number. If the user enters an X, the calculator is turned off. The various input values (i.e. numbers, operators, commands) will be followed by the ENTER key. Your program should prompt the user on what the user is to do. The commands C and X may be entered in place of an operator. All numbers entered and displayed will be integer values

NOTES:

The person using the calculator has a terrible time using a keyboard and may not always enter a valid operator. If this happens the program should ask for another operator.

Regardless of math operation attempted, the program must not die. If the math operation would cause a fatal error in your program you should check for it and issue an error message rather than attempting the operation.

Explanation / Answer

#include<stdio.h>

int main(){
int num1 = 0, num2 = 0, res = 0;
char input;

printf("Enter the first number : "); //Take the first number from the user
scanf("%d",&num1);
res = num1;

while(1){ // This loop will continue till the user enters the character 'X'
int flag = 1; //flag variable to check wheather a valid operator is inputed or not
while(flag){
fflush(stdin); // fflush used to flush the standard input output
printf("Enter the operator : ");
scanf("%c",&input);
if(input == '+' || input == '-' || input == '*' || input == '/'){ // if the input character is among any of the valid opeartor then change flag to 0
flag = 0;
}else if(input == 'C'){ // If input opeartor is 'C' then clear the previous result and again ask the user for the first number
res = 0;
printf("Enter the first number : ");
scanf("%d", &num1);
}else if(input == 'X'){ // If input character is 'X' then the program ends
printf("Turning OFF .........");
return 0;
}
else{ // For any other input character ask for another valid value
printf("Enter valid opeartor");
}
}
printf("Enter the second number : "); // Take the second number
scanf("%d", &num2);

switch(input){ // Perform the required opeartion
case '+' :
res += num2;
break;
case '-' :
res -= num2;
break;
case '*' :
res *= num2;
break;
case '/' :
res /= num2;
}

printf("%d ",res); // output the result
}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote