C Program for a Simple Calculator

The program for a simple calculator is a demonstration of arithmetic operations in C programming language. A user inputs two numbers and selects an operation to perform. The program computes the results and prints the output to the console.

We wrote the program using Dev-C++ version 4.9.9.2 compiler installed on a Windows 7 64-bit system. You may try other compilers such as Turbo C++ 3.0.

Get yourself familiar with following c programming concepts before learning this program.

Program Definition

This is a simple basic calculator with mathematical operations. The program accepts two numbers and then display C calculator options.

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

You choose one option from the above 6 and then the program performs those actions. Finally, it prints the results.

The C calculator options are implemented using the switch-case concept of C language. Whenever a case number matches the program perform those actions.

Each of the actions is a function whose result is printed immediately.

Flowchart

Flowchart-Calculator Program
Flowchart-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 the respective operation on user inputs and return the results to the console. The following statements work on the operators to get the results.

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

Output

The program shows options when you execute it successfully. Before you choose any option, enter two input numbers.

Output1-Calculator-Input
Output1-Calculator-Input

 

Output2-Calculator-Addition
Output2-Calculator-Addition

Related Article:

Advertisements