Question: TECH 1211 Computer Programming Homework Program 7 Convert program with a Switch to a Cascaded If Objectives: 1. To introduce the student to the character
TECH 1211 Computer Programming Homework Program 7 Convert program with a Switch to a Cascaded If Objectives: 1. To introduce the student to the character data type, %c 2. To familiarize the student with the basic Switch statement 3. To convert the branching program from a Switch statement to a collection of nested if statements also called Cascaded If statements.
Background: This program requires the user to first enter the arithmetic operation to be performed and then the two floating point numbers to be operated on. Once the two numbers are entered, the program executes the operation and displays the result of that operation.
Listing
int main()
{
char operator;
float firstNumber,secondNumber;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%f %f",&firstNumber, &secondNumber);
switch(operator)
{
case '+':
printf("%lf + %lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber);
break;
case '-':
printf("%f - %.1lf = %f",firstNumber, secondNumber, firstNumber - secondNumber);
break;
case '*':
printf("%f * %f = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber);
break;
case '/':
printf("%f / %f = %f",firstNumber, secondNumber, firstNumber / firstNumber);
break;
// operator doesn't match any case constant (+, -, *, /)
default:
printf(The operator must have been entered incorrectly );
}
return(0);
}
Convert this program from one that uses a Switch to an equivalent that uses a cascaded if. Output Screen:
Note:A new type of data is introduced, char. Variables that are used to represent data that is text in nature (i.e. symbols or letters) is defined in the program prior to the use of the variable.
Save and upload to ecourseware this program using the file name: YourLastName_YourFirstName_Program_7_Convert_Switch_to_Cascaded_If.c
Although it is not in the sample output, make sure to put your name in both the program listing and the output screen as you have been doing all semester.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
