Question: Write a for-loop in C using the instructins below for this code: /* This C program demonstrates the switch statement without using breaks. */ /*
Write a for-loop in C using the instructins below for this code:
/* This C program demonstrates the switch statement without using breaks. */
/* The program is tested on MS Visual C++ platform */
#include
#pragma warning(disable : 4996)
void main() {
char ch;
ch = '+';
int f, a = 10, b = 20;
printf("ch = %c ", ch);
switch (ch) {
case '+': f = a + b; printf("f = %d ", f);
case '-': f = a - b; printf("f = %d ", f);
case '*': f = a * b; printf("f = %d ", f);
case '/': f = a / b; printf("f = %d ", f);
default: printf("invalid operator ");
}
ch = '-';
printf("ch = %c ", ch);
switch (ch) {
case '+': f = a + b; printf("f = %d ", f);
case '-': f = a - b; printf("f = %d ", f);
case '*': f = a * b; printf("f = %d ", f);
case '/': f = a / b; printf("f = %d ", f);
default: printf("invalid operator ");
}
ch = '*';
printf("ch = %c ", ch);
switch (ch) {
case '+': f = a + b; printf("f = %d ", f);
case '-': f = a - b; printf("f = %d ", f);
case '*': f = a * b; printf("f = %d ", f);
case '/': f = a / b; printf("f = %d ", f);
default: printf("invalid operator ");
}
ch = '/';
printf("ch = %c ", ch);
switch (ch) {
case '+': f = a + b; printf("f = %d ", f);
case '-': f = a - b; printf("f = %d ", f);
case '*': f = a * b; printf("f = %d ", f);
case '/': f = a / b; printf("f = %d ", f);
default: printf("invalid operator ");
}
ch = '%';
printf("ch = %c ", ch);
switch (ch) {
case '+': f = a + b; printf("f = %d ", f);
case '-': f = a - b; printf("f = %d ", f);
case '*': f = a * b; printf("f = %d ", f);
case '/': f = a / b; printf("f = %d ", f);
default: printf("invalid operator ");
}
}

Expected Output:

2.3 Use a for-loop in the code given above to accept 5 user inputs. Use an input statement such as ch = fgetchar0: to replace the assignment statement ch'+'; You can use either GCC or Visual Studio to complete this question. Please refer to the expected output word document for further clarification. Submit the rewritten progranm 10 points] Note, you may need to use fflush(stdin) to flush the newline character left behind by a previous operation before using fflush(stdin)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
