Menu-Driven C Program with Switch Case

Beginner

Introduction

In this lab, we will create a menu-driven program using switch case in C. A menu-driven program is a program that presents a menu to the user, and the user chooses an option from the menu to perform a specific task. In this program, we will create a menu that will allow the user to perform various tasks such as calculating the factorial of a number, checking if a number is prime or composite, and checking if a number is even or odd.

Create a New C file

The first step is to create a new C file in the ~/project/ directory. Name this file main.c.

Including Required Libraries

In this step, we will include the standard input and output library in our program. This library provides functions for input and output operations in C.

#include <stdio.h>

Defining the main() function

main() is the entry point function in a C program. In this step, we will define the main() function.

int main()
{
    // Code goes here
    return 0;
}

In this step, we will display the menu to the user. The menu will allow the user to choose from different options. For this program, we will use the following options:

  • Option 1: Calculate the Factorial of a Number
  • Option 2: Check if a Number is Prime
  • Option 3: Check if a Number is Even or Odd
  • Option 4: Exit Program
int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);
    }
    return 0;
}

Implementing Switch Case

In this step, we will implement the switch case statement. Based on the user's choice, we will execute different cases.

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                /*
                * Code to calculate factorial of a number
                */
                break;

            case 2:
                /*
                * Code to check if a number is prime or composite
                */
                break;

            case 3:
                /*
                * Code to check if a number is even or odd
                */
                break;

            case 4:
                /*
                * Code to exit the program
                */
                break;
        }
    }
    return 0;
}

Calculate the Factorial of a Number

In this step, we will calculate the factorial of a number requested by the user when the user selects option 1.

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                printf("Enter number:\n");
                scanf("%d", &num);
                fact = 1;
                for(i = 1; i <= num; i++)
                {
                    fact = fact*i;
                }
                printf("\n\nFactorial value of %d is = %lu\n\n\n",num,fact);
                break;

            case 2:
                /*
                * Code to check if a number is prime or composite
                */
                break;

            case 3:
                /*
                * Code to check if a number is even or odd
                */
                break;

            case 4:
                printf("\n\n\t\t\tCoding is Fun !\n\n\n");
                exit(0);
        }
    }
    return 0;
}

Check if a Number is Prime

In this step, we will check if a number is prime or composite. The program will output a message indicating whether the number entered by the user is prime or not.

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                /*
                * Code to calculate factorial of a number
                */
                break;

            case 2:
                printf("Enter number:\n");
                scanf("%d", &num);
                if(num == 1)
                    printf("\n1 is neither prime nor composite\n\n");
                for(i = 2; i < num; i++)
                {
                    if(num%i == 0)
                    {
                        printf("\n%d is not a prime number\n\n", num);
                        break;
                    }

                }
                /*
                    Not divisible by any number other
                    than 1 and itself
                */
                if(i == num)
                {
                    printf("\n\n%d is a Prime number\n\n", num);
                    break;
                }
                break;

            case 3:
                /*
                * Code to check if a number is even or odd
                */
                break;

            case 4:
                printf("\n\n\t\t\tCoding is Fun !\n\n\n");
                exit(0);
        }
    }
    return 0;
}

Check if a Number is Even or Odd

In this step, we will check if a number is even or odd. The program will output a message indicating whether the number entered by the user is even or odd.

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                /*
                * Code to calculate factorial of a number
                */
                break;

            case 2:
                /*
                * Code to check if a number is prime or composite
                */
                break;

            case 3:
                printf("Enter number:\n");
                scanf("%d", &num);

                if(num%2 == 0) // 0 is considered to be an even number
                    printf("\n\n%d is an Even number\n\n",num);
                else
                    printf("\n\n%d is an Odd number\n\n",num);
                break;

            case 4:
                printf("\n\n\t\t\tCoding is Fun !\n\n\n");
                exit(0);
        }
    }
    return 0;
}

Exit Program

In this step, we will add a case to exit the program when the user selects option 4.

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                /*
                * Code to calculate factorial of a number
                */
                break;

            case 2:
                /*
                * Code to check if a number is prime or composite
                */
                break;

            case 3:
                /*
                * Code to check if a number is even or odd
                */
                break;

            case 4:
                printf("\n\n\t\t\tCoding is Fun !\n\n\n");
                exit(0);
        }
    }
    return 0;
}

Summary

In this lab, we have created a menu-driven program in C using switch case. This program displays a menu to the user, and the user can choose from different options to perform various tasks such as calculating the factorial of a number, checking if a number is prime or composite, and checking if a number is even or odd. We hope this lab has provided you with a good understanding of creating a menu-driven program using switch case in C.

Other Tutorials you may like