C Programming Language Goals • Implement a switch statement • Convert between in
ID: 3696815 • Letter: C
Question
C Programming Language
Goals
• Implement a switch statement • Convert between integer and floating point math • Implement a do/while loop • Use character input and logical operators
Tasks
1. Write a program that does the following:
a. Ask the user to enter two integers greater than 0 and less than or equal to 100
i. Use one or more logical operator(s) && || ! to check for the appropriate range
ii. If the number entered is outside of the range, print an error message asking the user to reenter a number (repeat as many times as necessary)
iii. Use a do/while loop to accomplish the repetition required above
iv. Hint: check for the range for the 1st number before asking for the 2nd number
b. Display a menu of choices for the operation to be done with the two numbers: a – addition s – subtraction m – multiplication d – division
c. Allow the user to select which operation they wish to perform
d. Use a switch statement to determine which action to take base on the user’s input.
e. If the user enters an invalid option, print an error message
f. Complete the selected operation and print the entire equation with the result (Note that the division could result in a non-integer value; display 2 decimal places).
Explanation / Answer
#include<stdio.h>
int main()
{
int a,b;
char op;
do
{
printf("Enter the first number:");
scanf("%d",&a);
}
while(a<=0||a>100);
do
{
printf("Enter the second number:");
scanf("%d%*c",&b);
}
while(b<=0||b>100);
printf("Choose the operation to be done with the two numbers: a for addition s for subtraction m for multiplication d for division ");
scanf("%c",&op);
switch(op)
{
case 'a':
printf("The sum of two numbers is %d ",a+b);
break;
case 's':
printf("The subtraction of two numbers is %d ",a-b);
break;
case 'm':
printf("The multiplication of two numbers is %d ",a*b);
break;
case 'd':
printf("The division of two numbers is %.2f ",a*1.0/b);
break;
default :
printf("Invalid choice " );
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.