C Menu Driven Program using Switch
In this tutorial, we will learn how to write a menu driven program in C programming language using the Switch.
In this tutorial, we will learn how to write a menu driven program in C programming language using the Switch.
Write a menu driven program which has the following options:
Once a menu item is selected, the appropriate action should be taken accordingly.
Prompt:
Area of Circle
Area of Rectangle
Area of Square
Area of Triangle
Input:
Enter your choice: 1
Enter radius: 10
Output:
Area of Circle = 314.16
Explanation:
The prompt says that if the user chooses 1, then the area of the circle will be calculated. To calculate the area of a circle, the user needs to provide the radius. Since the radius is 10, the result is 314.16, which is the area of the circle with radius 10.
Prompt:
Area of Circle
Area of Rectangle
Area of Square
Area of Triangle
Input:
Enter your choice: 2
Enter length: 10
Enter breadth: 20
Output:
Area of Circle = 200
Explanation:
If the user chooses 2, then the area of the rectangle will be calculated. To calculate the area, the user needs to enter the length and the breadth. The area of the rectangle is length * breadth, which is 200.
You need to write the complete program using the following template as the starter:
#include <stdio.h>
int main() {
int dec;
// The prompt.
printf("Enter the number: ");
scanf("%d", &dec);
// Write your code here.
return 0;
}
Copy and paste the above code in your favorite IDE and start writing your code after the comment “// Write your code here.
”
Pro tip: First, try writing the code on your own. Try every possible thing you can to come up with the solution. If you still can’t figure it out, then check the solution and see what went wrong. This is how you learn. 🙂
To verify the program you have written, go ahead and test your program using the following test cases:
Input | Output | |
---|---|---|
Test Case 1 | 0 | Quiet |
Test Case 2 | 10000 | This value is off the charts! Try again. |
Test Case 3 | 100 | Very loud |
Test Case 4 | -50 | Negative value is not allowed. |
Don’t forget to try the code on your own before diving into the solution.
Following are the multiple approaches to the same problem:
We can use if-else ladder to check multiple conditions as shown in the following code:
#include <stdio.h>
int main() {
int dec;
// The prompt.
printf("Enter the number: ");
scanf("%d", &dec);
// Write your code here.
if (dec < 0) {
printf("Negative value is not allowed.\n");
} else if (dec >= 0 && dec <= 60) {
printf("Quiet\n");
} else if (dec > 60 && dec <= 70) {
printf("Conversational\n");
} else if (dec > 70 && dec <= 90) {
printf("Loud\n");
} else if (dec > 90 && dec <= 110) {
printf("Very Loud\n");
} else if (dec > 110 && dec <= 129) {
printf("Dangerous\n");
} else if (dec > 129 && dec <= 194) {
printf("Very Dangerous\n");
} else {
printf("This value is off the charts! Try again.");
}
return 0;
}
Output:
Enter the number: 10
Quiet
You can check the above program for multiple test cases and the code works just fine.
If you closely observe the previous code, there is no need to check the first condition in every ‘if’ statement. For example, it is clear that when dec
is not less than 60, then it must be greater than 60. This is because we are using an ‘if-else’ ladder due to which a specific conditional expression will only be checked when the conditions prior to it are not satisfied (or False
). So, for instance, it is clear that the conditional expression dec > 60 && dec <= 70
will be evaluated only when dec < 0
and dec >= 0 && dec <= 60
fails. Hence, there is no need to check the first condition in each conditional expression.
The following program is the modified version of the previous program:
#include <stdio.h>
int main() {
int dec;
// The prompt.
printf("Enter the number: ");
scanf("%d", &dec);
// Write your code here.
if (dec < 0) {
printf("Negative value is not allowed.\n");
} else if (dec <= 60) {
printf("Quiet\n");
} else if (dec <= 70) {
printf("Conversational\n");
} else if (dec <= 90) {
printf("Loud\n");
} else if (dec <= 110) {
printf("Very Loud\n");
} else if (dec <= 129) {
printf("Dangerous\n");
} else if (dec <= 194) {
printf("Very Dangerous\n");
} else {
printf("This value is off the charts! Try again.");
}
return 0;
}
Output:
Enter the number: 0
Quiet
There is even a better way to write the program for this problem according to me. We can replace the if-else statements with switch as shown in the following program:
#include <stdio.h>
int main() {
int dec;
// The prompt.
printf("Enter the number: ");
scanf("%d", &dec);
// Write your code here.
if (dec < 0) {
printf("Negative value is not allowed.\n");
} else {
switch(dec) {
case 0 ... 60:
printf("Quiet\n");
break;
case 61 ... 70:
printf("Conversational\n");
break;
case 71 ... 90:
printf("Loud\n");
break;
case 91 ... 110:
printf("Very Loud\n");
break;
case 111 ... 129:
printf("Dangerous\n");
break;
case 130 ... 194:
printf("Very Dangerous\n");
break;
default:
printf("This value is off the charts! Try again.\n");
}
}
return 0;
}
Output:
Enter the number: -10
Negative value is not allowed.
The program above is the same as the previous ones in terms of logic. The only difference is that the if-else ladder is replaced by a switch statement. You may have noticed the three dots (…) in every case mentioned. These three dots are used to represent a specific range. The rest of the code is self-explanatory.
Leave a comment