rite a C program that performs the five basic integer arithmetic operations on t
ID: 3647149 • Letter: R
Question
rite a C program that performs the five basic integer arithmetic operations on two input numbers. The program should begin by accepting two integer inputs with values on the closed interval [-1000, 1000]. This restraint should be enforced by performing the appropriate bounds checking. The program should then present a menu that allows the user to select an integer operation (addition, subtraction, multiplication, division or modulus). Again, appropriate bounds checking should be performed on this menu selection. The program performs the indicated operation on the two input numbers and displays the result in an attractive display.Use the following set of data when creating the three screenshots submitted with the program:
Operation Operand #1 Operand #2
Trial #1 Division 10 3
Trial #2 Modulus -23 4
Trial #3 Subtraction 999 -1024
Your program must include all of the following aspects:
1.) at least one if, if-else, or if-else chain flow control structure
2.) at least one switch-case flow control structure
3.) at least one example of integer bounds checking
4.) at least one example of explicit type-casting
5.) at least one example of handling the phantom enter key
Your program must conform to the following restrictions:
1.) You may only use the printf() and scanf() functions for input and output.
2.) You must include at least one example of accepting integer input and at least one example of accepting character input (use both the %d and %c conversion characters with scanf() ).
3.) When invalid input is supplied to the program, it should exit gracefully (via a return statement).
Additional Notes:
1.) You may assume that the user will type in integer values when prompted to do so. You may not make any assumptions about the values of the integers the user types in.
2.) You should include logic that handles special cases. For example, division by the integer
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <conio.h>
int main ()
{int o1,o2,error;
char op;
printf("NOTE: You must enter integer operands between -1000 and 1000 ");
printf("Please enter the first operand: ");
scanf("%d",&o1);
while(o1<-1000||o1>1000)
{printf("Must be between -1000 and +1000 ");
printf("Please reenter the first operand: ");
scanf("%d",&o1);
}
printf("Please enter the second operand: ");
scanf("%d",&o2);
while(o2<-1000||o2>1000)
{printf("Must be between -1000 and +1000 ");
printf("Please reenter the second operand: ");
scanf("%d",&o2);
}
while(getchar()!=' ');
do
{error=0;
printf("Please select an operation to place in the blank space: ");
printf("%d ___ %d= ",o1,o2);
printf("A - Addition ");
printf("S - Subtraction ");
printf("M - Multiplication ");
printf("D - Division ");
printf("O - Modulus ");
printf(":");
scanf("%c",&op);
printf("-------------------- ");
switch(op)
{case 'A': case 'a':
printf("%d + %d = %d ",o1,o2,o1+o2);
break;
case 'S': case 's':
printf("%d - %d = %d ",o1,o2,o1-o2);
break;
case 'M': case 'm':
printf("%d * %d = %d ",o1,o2,o1*o2);
break;
case 'D': case 'd':
if(o2==0)
printf("cannot divide by 0 ");
else
printf("%d / %d = %.4f ",o1,o2,o1/(double)o2);
break;
case 'O': case 'o':
printf("%d %% %d = %d ",o1,o2,o1%o2);
break;
default: printf("Invalid operation ");
error=1;
}
}while(error==1);
printf(" Press any key to exit");
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.