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.


Program

Write a menu driven program which has the following options:

  1. Calculate area of circle
  2. Calculate area of rectangle
  3. Calculate area of square
  4. Calculate area of triangle

Once a menu item is selected, the appropriate action should be taken accordingly.

Example 1:

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.

Example 2:

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.

Expectations:

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. 🙂

Sample Test Cases:

To verify the program you have written, go ahead and test your program using the following test cases:

InputOutput
Test Case 10Quiet
Test Case 210000This value is off the charts! Try again.
Test Case 3100Very loud
Test Case 4-50Negative value is not allowed.

Don’t forget to try the code on your own before diving into the solution.


Solution

Following are the multiple approaches to the same problem:

The Easiest Approach

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.

More Refined Approach

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

The switch Statement Approach

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

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.