Menu-Driven Program in C using Switch Case

Menu-driven program in c: Here are 9 menu-driven programs using switch case, do-while loop, while loop and also using if-else.

while(!(succeed=try()));

In this post, we will make many Menu-Driven Program in C for different tasks like simple calculator and a menu driven program to calculate area of different shapes. 

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

    Simple Calculator program in c using switch case and do-while loop

    In this c program, we are going to perform arithmetic operations (i.e., addition, subtraction, multiplication, and division) between two numbers in c language using switch case and do-while loop
    Here, we will make a Simple Calculator program in c using a switch case.

    Menu driven program in c for addition, subtraction, multiplication, division

    #include<stdio.h>
    void main()
    {
    int a,b,ch;
    char choice;
    printf("\n Enter two numbers:");
    scanf("%d%d",&a,&b);
    do{
    printf("\n Press 1 to add two numbers ");
    printf("\n Press 2 to subtract two numbers ");
    printf("\n Press 3 to multiply two numbers ");
    printf("\n Press 4 to divide two numbers \n");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1: printf("Sum: %d",a+b);
     		break;
    case 2: printf("Subtract :%d",a-b);
    		break;
    case 3: printf("Multiply :%d",a*b);
     		break;
    case 4: if(b==0)
    		printf("\n Denominator cannot be zero");
            else
    		printf("Divide :%d",a/b); // b should not be zero
    		break;
    default:printf("Wrong choice!");
    }
    printf("\n Do you want to continue? (Press y/n)");
    scanf(" %c",&choice);
    }while(choice=='y');
    }
    

    The output of a simple calculator program in c using switch case and do-while loop:

    Enter two numbers:4 3
    Press 1 to add two numbers 
    Press 2 to subtract two numbers 
    Press 3 to multiply two numbers 
    Press 4 to divide two numbers 
    3 Multiply:12
    Do you want to continue? (Press y/n)n

    do-while loop

    do-while loop is also known as exit-controlled loop. It first executes the loop first time without checking the test expression and then at the bottom it checks the testing condition. If the testing condition is true then it executes the loop again otherwise it doesn’t execute the loop again.

    Approach to make a simple calculator program in c using switch case and do-while loop:-

    1. Initialize three numbers, one number is for the choice of the user and we perform arithmetic operations on the other two numbers.
    2. Input two numbers on which we perform arithmetic operations.
    3. Print all the choices and input the choice of the user.
    4. Using the switch keyword, we write all the four cases for addition, subtraction, multiplication, and division respectively.
    5. Do not forget to write a break at the end of the case statement.
    6. You can also write a default statement in case the user input the wrong choice
    7. for example: - default:printf("Wrong choice!");

    Menu-driven program in c using switch case

    #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 area of Circle
    Press 2 to calculate area of Rectangle
    Press 3 to calculate area of Square

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

    Menu-driven in c using while loop

    In this program, we are going to make a menu-driven program in c for a simple calculator using a while loop and using an if-else statement instead of a switch-case statement. If you want the source code of the menu-driven program in c using while loop and switch case, then let me know in the comment section.

    Menu-driven program in c using if-else

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int num1, num2, choice;
    while(1)
        {
            printf("\n Menu Driven Program in c");
            printf("\n 1.Addition");
            printf("\n 2.Subtraction");
            printf("\n 3.Multiplication");    
            printf("\n 4.Division");
            printf("\n 5.exit");
            printf("\n Enter Choice:");
            scanf("%d",&choice);    
        if(choice>0 && choice<6)
        {
        if (choice==1)
            {
                printf("\n Enter 1st number: ");
                scanf("%d",&num1);
                printf("\n Enter 2nd number: ");
                scanf("%d",&num2);
                printf("\n %d + %d = %d",num1,num2,num1+num2);
            }
        if (choice==2)
            {
                printf("\n Enter 1st number: ");
                scanf("%d",&num1);
                printf("\n Enter 2nd number: ");
                scanf("%d",&num2);
                printf("\n %d - %d = %d",num1,num2,num1-num2);
            } 
        
        if (choice==3)
            {
                printf("\n Enter 1st number: ");
                scanf("%d",&num1);
                printf("\n Enter 2nd number: ");
                scanf("%d",&num2);
                printf("\n %d * %d = %d",num1,num2,num1*num2);
            } 
         
        
        if (choice==4)
            {
                printf("\n Enter 1st number: ");
                scanf("%d",&num1);
                printf("\n Enter 2nd number: ");
                scanf("%d",&num2);
                if(num2==2)
                printf("\n Denominator cannot be zero");
                else
                {
                    printf("\n %d / %d = ",num1,num2);
                    printf("%d",num1/num2);
                }
            }
        if(choice==5)
            exit(0);
        }            
        else printf("\n Wrong choice");
        }
        return 0;
    }

    Output:

    Menu Driven Program in c using while-loop and if-else


    1.Addition
     2.Subtraction
     3.Multiplication
     4.Division
     5.exit
     Enter Choice:1
     Enter 1st number: 5
     Enter 2nd number: 6
     5 + 6 = 11

    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.

    Menu-driven program in c

    #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 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 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 Simple Calculator program in c using a switch case. This program is an answer to one of the most important questions that arise in the book "Let Us C" by Yashwant Singh Kandari.
         
        Thanks for reading the Menu driven program in c using switch case. If you like it then please comment below to support us.
     
    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)

    Siddharth Jha
    • Software Developer • Website Developer • Masters in SEO • Social Media Manager

    Related Posts

    7 comments

    Post a Comment

    Follow by Email