Menu-Driven Program in C using Switch Case

In this post, we will make many Menu-Driven Programs in C for different tasks like a program to calculate the area of different shapes, perform the given tasks and arithmetic operations between two matrices. 

Menu-driven program in c: A program that obtains choice from a user by displaying the menu.

Here are 3 menu-driven programs using switch case, do-while loop, and while loop.

If you are unable to run this program or getting an error, let me know in the comment section. You can also message us on our Instagram account.

    Menu-driven program in c using Switch Case

    In this program, we are going to create a menu-driven program in c that will print the area of different shapes according to the user's choice.

    #include<stdio.h>
    int main()
    {
    int choice, radius, length, breadth, side;
    float area;
    printf("\n Press 1 to calculate area of Circle  \n Press 2 to calculate area of Rectangle \n Press 3 to calculate area of Square \n Press 4 to Exit");
    printf("\n Enter your choice:");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1: printf("\n Enter radius:");
             scanf("%d",&radius);
             area=3.14*radius*radius;
             printf("\n Area of circle: %f",area);
            break;
    case 2: printf("\n Enter length:");
             scanf("%d",&length);
             printf("\n Enter breadth:");
             scanf("%d",&breadth);
             printf("\n Area of rectangle: %d",length*breadth);
            break;
    case 3: printf("\n Enter Side:");
             scanf("%d",&side);
             printf("\n Area of square: %d",side*side);
            break;
    case 4: return 0;
    default: printf("\n wrong choice");
    }
    return 0;
    }

    Output:

    Press 1 to calculate the area of Circle
    Press 2 to calculate the area of Rectangle
    Press 3 to calculate the area of Square

    Press 4 to Exit
    Enter your choice:1
    Enter radius:5
    Area of circle: 78.500000


    Menu-driven program in c

    Menu-driven program in c using switch case and do-while loop which performs the following operations:
    1. C program to check whether the number is prime or not.
    2. Factorial program in c using for loop.
    3. C program to check whether the number is odd or even.
    #include<stdio.h>
    int main()
    {
    char fav; int num=0,choice=1,fact=1,i;
    printf("\n Enter a Number:");
    scanf("%d",&num);
    do
    {
    printf("\n Press 1 to find factorial of a number \n Press 2 to check whether the number is Prime or Not \n Press 3 to find check number is even or odd \n Press 4 to Exit"); printf("\n Enter your choice:");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1: for(i=num;i>1;i--)
                 fact*=i;
                printf("\n Factorial of %d",num);
                printf(" is %d",fact);
                break;
    case 2:for(i=2;i<num;i++)
                {
                 if(num%i==0)
                {
                printf(" \n %d is not a prime number",num);
                break;
                }
                }
                if(num==i)
                printf("%d is a prime number",num);
                break;
    case 3: if(num%2==0)
                printf("%d is an even number",num);
                else
                printf("\n %d is an odd number",num);
                break;
    case 4: return 0;
    default: printf("\n wrong choice");
    }
    printf("\n Do you want to continue?(y/n)\n");
    scanf("\n %c",&fav);
    }while(fav=='y');
    return 0;
    }
    Menu driven program in c using switch case and do-while loop Output of c program to check whether the number is odd or even

    Algorithm for Menu-driven program in c

    • Initialize three int variables and a one-character variable.
    • Now take the number from the user and store it in an int variable.
    • Now print all the choices on the screen and take the choice of the user using another int variable.
    • Using statement switch(choice), the choice is an int variable that contains the choice of the user. (you can write any valid identifier)
    • Make statements of case 1, case 2, case 3, etc using for loop and if-else statement, and at the end of every case statements write a break. So that it doesn’t go for the next statement.
    • Write a default statement in case the user enters the wrong choice.
    • Now display a message on the screen that do you want to continue?
    •  If the user wants to continue then they should press y.
    • Take the input from the user in the character variable and don’t forget to press space between   and  in the scanf statement.
    • In the end, you have to write the testing statement for the do-while loop.

    Menu-driven program for matrix operations in c using switch case

    In this menu-driven program for matrix operations in c, we will perform all three operations i.e. addition, subtraction, and multiplication of matrices using a 2-D array.


    #include<stdio.h>
    int main()
    {
    int C[3][3],A[3][3],B[3][3],i,j,ch;
    
    printf("Menu-driven program for matrix operations in c");
    printf("\n Matrix A\n");
    printf("\n Enter the numbers you want to insert in the matrix A:");
    for(i=1;i<=2;i++)
    {
    for(j=1;j<=2;j++)
    {
    printf("\t");
    scanf("%d",&A[i][j]);
    }
    }
    printf("\n Matrix A:\n");
    for(i=1;i<=2;i++)
    {
    for(j=1;j<=2;j++)
    {
    printf("%d",A[i][j]);
    printf("\t");
    }
    printf("\n");
    }
    printf("\n Enter the numbers you want to insert in the matrix B:");
    for(i=1;i<=2;i++)
    {
    for(j=1;j<=2;j++)
    {
    printf("\t");
    scanf("%d",&B[i][j]);
    }
    }
    printf("\n Matrix B:\n");
    for(i=1;i<=2;i++)
    {
    for(j=1;j<=2;j++)
    {
    printf("%d",B[i][j]);
    printf("\t");
    }
    printf("\n");
    }
    printf("\n 1. Addition of Matrix A & B ");
    printf("\n 2. Subtraction Of Matrix A and matrix B n");
    printf("\n 3. Multiplication of Matrix A & B");
    printf("\n Enter your choice (1 ,2,3):");
    scanf("\n %d",&ch);
    switch(ch)
    {
     case 1:printf("\n A+B=\n");
     for(i=1;i<=2;i++)
     {
     for(j=1;j<=2;j++)
     {
     C[i][j]=A[i][j]+B[i][j];
     printf("%d",C[i][j]);
     printf("\t");
     }
     printf("\n");
     }
     break;
     case 2:printf("\n A-B=\n");
     for(i=1;i<=2;i++)
     {
     for(j=1;j<=2;j++)
     {
     C[i][j]=A[i][j]-B[i][j];
     printf("%d",C[i][j]);
     printf("\t");
     }
     printf("\n");
     }
     break;
     case 3:printf("\n A*B=\n");
     for(i=1;i<=2;i++)
     {
     for(j=1;j<=2;j++)
     {
     C[i][j]=(A[i][1]*B[1][j])+(A[i][2]*B[2][j]);
     printf("%d",C[i][j]);
     printf("\t");
     }
     printf("\n");
     }
     break;
     default: printf("\n Wrong Choice");
    }
    return 0; 
    }
    
     

    If you are looking for a Menu-driven program for matrix operations in c using if-else, let me know in the comment section.

    Output:
    Menu-driven program for matrix operations in c
    Matrix A
    Enter the numbers you want to insert in the matrix A: 1
    2
    3
    4
    Matrix A:
    1 2
    3 4
    Enter the numbers you want to insert in the matrix B: 1
    2
    3
    4
    Matrix B:
    1 2
    3 4
    1. Addition of Matrix A & B
    2. Subtraction Of Matrix A and matrix B
    3. Multiplication of Matrix A & B
    Enter your choice (1, 2, 3):1
    A+B=
    2 4
    6 8

    About menu-driven program in c using switch case

    In this post, we learn to make a menu-driven program to print areas of circles, squares and rectangles. We also perform matrix operations using a switch case and do-while loop. The second program is an answer to one of the most important questions that arise in the book "Let Us C" by Yashwant Singh Kandari. 

    If you like this then please comment below to support us and you can freely ask your doubts in the comment section.
     
    This program will clear your concepts about switch case programs, do-while loop, for loop, if-else statement, and exit function.

    The solution of scanf (“%c”,&ch) not working

    Important note about taking the value of character from a user in c using scanf
    scanf(“%c”,&ch)


    This is the wrong statement but this error is ignored by the compiler when you compile this statement in your program, it will not show any errors regarding this. Moreover, when you execute your program this statement will not be executed.

    Therefore you have to press space between and % means you have to use the following statement:

    scanf(“ %c”,&ch)

    Related Posts

    9 comments

    1. Tum bhot acha kaam karta h😂😂

      ReplyDelete
    2. Ha bhai
      Tujhe bhi ek baar menu driven program in c using switch case bnana chahiye

      ReplyDelete
    3. Thanks shivam Gupta, share this post to your friends.
      Your comment is very valuable for us.

      ReplyDelete
    4. This is fascinating menu driven program in c💯

      ReplyDelete
    5. I was trying so many times bt noting worked bt thank you so much for giving solution on "The solution of scanf (“%c”,&ch) not working" this. I used solution given by you
      & code run perfectly.

      ReplyDelete

    Post a Comment