Using C Programming Language HW #7 Due Saturday, 2/25 by 11:30 PM Write a progra
ID: 3796426 • Letter: U
Question
Using C Programming Language
HW #7 Due Saturday, 2/25 by 11:30 PM Write a program that shows you a menu offering you the choice of addition, subtraction, multiplication, or division. After getting your choice, the program asks for two numbers, then performs the requested operation, prints out the expression evaluated and its result. The program should accept only the offered menu choices. It should use type float or double for the numbers and allow the user to try again if he or she fails to enter a number. In the case of division, the program should prompt the user to enter a new value if 0 is entered as the value for the second number. A typical program run should look like this:Explanation / Answer
// C code
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
char menu()
{
char choice;
printf("Enter the operation of your choice: ");
printf(" a.add s.subtract m.multiply d.divide q.quit ");
scanf(" %c",&choice);
return choice;
}
int main()
{
double number1, number2;
while(1)
{
char choice = menu();
if(choice == 'a')
{
printf("Enter first number: ");
while(1)
{
if(scanf("%lf",&number1))
break;
else
printf("Please enter a number, such as 2.5. -1.788 or 3: ");
}
printf("Enter second number: ");
while(1)
{
if(scanf("%lf",&number2))
break;
else
printf("Please enter a number, such as 2.5. -1.788 or 3: ");
}
printf("%0.2lf + %0.2lf = %0.2lf ",number1, number2, (number1+number2));
}
else if(choice == 's')
{
printf("Enter first number: ");
while(1)
{
if(scanf("%lf",&number1))
break;
else
printf("Please enter a number, such as 2.5. -1.788 or 3: ");
}
printf("Enter second number: ");
while(1)
{
if(scanf("%lf",&number2))
break;
else
printf("Please enter a number, such as 2.5. -1.788 or 3: ");
}
printf("%0.2lf - %0.2lf = %0.2lf ",number1, number2, (number1-number2));
}
else if(choice == 'm')
{
printf("Enter first number: ");
while(1)
{
if(scanf("%lf",&number1))
break;
else
printf("Please enter a number, such as 2.5. -1.788 or 3: ");
}
printf("Enter second number: ");
while(1)
{
if(scanf("%lf",&number2))
break;
else
printf("Please enter a number, such as 2.5. -1.788 or 3: ");
}
printf("%0.2lf * %0.2lf = %0.2lf ",number1, number2, (number1*number2));
}
else if(choice == 'd')
{
printf("Enter first number: ");
while(1)
{
if(scanf("%lf",&number1))
break;
else
printf("Please enter a number, such as 2.5. -1.788 or 3: ");
}
printf("Enter second number: ");
while(1)
{
if(scanf("%lf",&number2))
break;
else
printf("Please enter a number, such as 2.5. -1.788 or 3: ");
}
while(number2 == 0)
{
printf("Enter a number other that 0: ");
scanf("%lf",&number2);
if(number2 == 0)
break;
}
printf("%0.2lf / %0.2lf = %0.2lf ",number1, number2, (number1/number2));
}
else if(choice == 'q')
{
printf("Bye. ");
break;
}
else
{
printf("Invalid Input ");
}
}
return 0;
}
/*
output:
Enter the operation of your choice:
a.add
s.subtract
m.multiply
d.divide
q.quit
a
Enter first number: 22.4
Enter second number: 1
22.40 + 1.00 = 23.40
Enter the operation of your choice:
a.add
s.subtract
m.multiply
d.divide
q.quit
m
Enter first number: 3
Enter second number: 4
3.00 * 4.00 = 12.00
Enter the operation of your choice:
a.add
s.subtract
m.multiply
d.divide
q.quit
s
Enter first number: 23
Enter second number: 4
23.00 - 4.00 = 19.00
Enter the operation of your choice:
a.add
s.subtract
m.multiply
d.divide
q.quit
d
Enter first number: 11
Enter second number: 0
Enter a number other that 0: 3
11.00 / 3.00 = 3.67
Enter the operation of your choice:
a.add
s.subtract
m.multiply
d.divide
q.quit
q
Bye.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.