Skip to content
Home » C Examples » C Program For A Simple Calculator

C Program For A Simple Calculator

    This example program accepts two input numbers and performs an arithmetic operation such as addition, subtraction, division, multiplication and mod operation on them. The output is printed to the console.

    Learn following C programming concepts before trying this example on your computer.

    Program Definition

    This calculator program is built using C arithmetic operators. The user is presented with a list of choices. Once the user input his or her choice that operation is performed.

    Here is the list of operations presented to the users.

    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division
    5. Modulo
    6. Close

    The C calculator is implemented using the switch-case mechanism of C language.

    Flowchart

    Flowchart-Calculator Program
    Figure 1 – Flowchart of Calculator Program

    Program Code

    /* Program for a simple calculator 
    in C using Switch_Case */
    #include <stdio.h>
    #include <conio.h>
    main()
    {
    /* Variable declarations */
            int i,a,b,choice,result;
            void menu(int a,int b,int choice);
            printf("ENTER TWO NUMBERS:n");
            printf("a=:");
            scanf("%d",&a);
            printf("b=:");
            scanf("%d",&b);
            i=0;
            while(choice != 6)
            {
                menu(a,b,choice);
                i++;
            }
            getch();
            return 0;
    }
    void menu(int a, int b, int choice)
    {
            int result,i;
            for(i=0;i < 35;i++)
            printf("*"); printf("\n\n");
            printf("\tC CALCULATOR\n\n");
            for(i=0;i < 35;i++)
            printf("_"); printf("\n\n");
            printf("1.ADDITION\n");
            printf("2.SUBTRACTION\n");
            printf("3.MULTIPLICATION\n");
            printf("4.DIVISION\n");
            printf("5.MODULO \n");
            printf("6.CLOSE\n");
            for(i=0;i < 35;i++)
            printf("_"); printf("\n\n");
            printf("Enter your Choice\n");
            scanf("%d",&choice);
            switch(choice)
            {
            case 1:
                result = a + b;
                for(i=0;i < 35;i++)
                printf("_"); printf("\n\n");
                printf("Result=%d\n",result);
    /* Addition Operation */
                printf("\n\n");
                break;
            case 2:
                result = a - b;
                for(i=0;i < 35;i++)
                printf("_"); printf("\n\n");
                printf("Result=%d\n",result); 
    /* Subtraction */
                printf("\n\n");
                break;
            case 3:
                result = a * b;
                for(i=0;i < 35;i++)
                printf("_"); printf("\n\n");
                printf("Result=%d\n",result); 
    /* Multiplication */
                break;
            case 4:
                result = a/b;
                for(i=0;i < 35;i++)
                printf("_"); printf("\n\n");
                printf("Result=%d\n",result); 
    /* Division operation */
                printf("\n\n");
                break;
            case 5:
                result = a % b;
                for(i=0;i < 35;i++)
                printf("_"); printf("\n\n");
                printf("Result=%d\n",result); 
    /* Modulo operation */
                printf("\n\n");
                break;
                default:
                exit(0);
        }
    }

    Each arithmetic operator performs their respective operation on user inputs and return the results. Here is the list of important expressions used in the program.

    result = a + b;
    result = a - b;
    result = a * b;
    result = a / b;
    result = a % b;

    Output

    The calculator program shows options when you run it. Enter two input numbers and then enter your choice of arithmetic operation.

    ENTER TWO NUMBERS:
    a=:33
    b=:55
    ****************************************
                   C CALCULATOR
    ________________________________________
    1.ADDITION
    2.SUBTRACTION
    3.MULTIPLICATION
    4.DIVISION
    5.MODULO
    6.CLOSE
    ________________________________________
    
    Enter your Choice
    _

    The output will be displayed on the screen.

                    C CALCULATOR
    ___________________________________________________
    1.ADDITION
    2.SUBTRACTION
    3.MULTIPLICATION
    4.DIVISION
    5.MODULO
    6.CLOSE
    ___________________________________________________
    Enter your Choice
    1
    ___________________________________________________
    Result=88