Menu Driven Program for Arithmetic operation in c programming


                                Menu driven program which means it provide the menu of operation to the user. For example in above definition it provide the menu of various arithmetic operation like addition, subtraction, etc.
For that we need to use switch statement.

                                In switch statement it provide different alternatives to the user to perform any action in the form of multiple case. And there is also a default statement which helpful to perform any default operation if user not choose any one from available options.

Syntax :

                       switch(<expression>)
                       {         
                                                           case <value-1> :
                                                                                   block-1
                                                                                   break;

                                                           
                                                            case <value-2> :
                                                                                   block-2
                                                                                   break;

                                                             default :
                                                                                  default block
                                                                                  break;
                        }

                                                              break keyword is use to break the program after particular case. So it prevent to performed unnecessary rest of the operations.



Source code for Menu Driven program


#include<stdio.h>

void main()
{
    int ch,no1,no2,result;
    clrscr();
    printf("Enter number 1 :");
    scanf("%d",&no1);

    printf("Enter number 2 :");
    scanf("%d",&no2);

    printf("\n");
    printf("1.Addition.\n");
    printf("2.Substaction.\n");
    printf("3.Multiplication.\n");
    printf("4.Division.\n");

    printf("Enter your choice here : ");
    scanf("%d",&ch);

    switch(ch)
    {
        case 1:
             result=no1+no2;
             printf("\nresult is : %d",result);
             break;

         case 2:
              result=no1-no2;
              printf("\nresult is : %d",result);
              break;

         case 3:
              result=no1*no2;
              printf("\nresult is : %d",result);
              break;
                 
         case 4:
              result=no1/no2;
              printf("\nresult is : %d",result);
              break;

         default:
              printf("\nplease enter valid choice");

    }
    getch();
}

Output:

menu driven program for arithmetic operator in c programming

0 comments:

Post a Comment