Write a program that uses the switch statement to perform the correct operation
ID: 3532074 • Letter: W
Question
- Write a program that uses the switch statement to perform the correct operation (addition, subtraction, multiplication or division). The program prompts the user to enter an operator (+, or -, or *, or /) and two integers. Based on the type of operator, the program computes the result: either adds or subtracts, or multiplies, or divides the two numbers.
- Write a program that asks the user to enter five integers and the program finds and displays the largest number of them. To write the program, please review the attached file that shows you an example program to find the smallest number.
Explanation / Answer
1=# include < stdio.h >
# include < conio.h >
main ( )
{
int a,b,choice,result; /* Declare variables */
clrscr ( );
printf ( "1. Addition ");
printf ( " 2. Subtraction ");
printf ( " 3. Multiplication ");
printf ( " 4. Division ");
printf( " Enter values of a,b=");
scanf ( %d%d", &a, &b); / * Input numbers variables */
printf ( " Enter your choice :-" );
scanf ( "%d" ,&choice); / * Input choice variable */
switch ( choice) /* Select any one choice */
{
Case 1:
result=a+b;
printf( " Addition = %d " ,result);
break;
Case 2:
result=a-b;
printf( "Subtraction = %d " ,result);
break;
Case 3:
result=a*b;
printf( "Multiplication = %d " ,result);
break;
Case 4:
result=a/b;
printf( " Division = %d " ,result);
break;
default:
printf ( " Your choice is wrong ");
}
getch( );
}
Output:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter values of a,b= 8 4
Enter your choice :- 2
Subtraction =4
2=
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.