C program to make calculator using switch case

In this example, you will gain step by step insight about C program to make calculator using switch case.

c program to make calculator

It is easy to implement the simple function of a calculator in C programming. The following code will have the simple function as addition, subtraction, multiplication, division and, square.

Please go through following articles of C programming to understand the concept of C program to make a simple calculator using switch case.

Example: C program to make calculator using switch case


/* C program to implement simple functions of a calculator*/

#include<stdio.h>

int main()
{
   int choice;
   long num1, num2, x;

   printf("Please choose your option:"
          "\n1 = Addition"
          "\n2 = Subtraction"
          "\n3 = Multiplication"
          "\n4 = Division"
          "\n5 = Squares"
          "\n6 = exit"
          "\n\nChoice: ");
   scanf("%d", &choice);

   //while loop check whether the choice is in the given range
   while(choice < 1 || choice > 6)
   {
      printf("\nPlease choose the above mentioned option."
             "\nChoice: ");
      scanf("%d", &choice);
   }

   switch (choice)
   {
   case 1:
      printf("Enter two numbers: \n");
      scanf("%ld %ld", &num1, &num2);
      x = num1 + num2;
      printf("Sum = %ld", x);
      break;

   case 2:
      printf("Enter two numbers: \n");
      scanf("%ld %ld", &num1, &num2);
      x = num1 - num2;
      printf("Subtraction = %ld", x);
      break;

   case 3:
      printf("Enter two numbers: \n");
      scanf("%ld %ld", &num1, &num2);
      x = num1 * num2;
      printf("Product = %ld", x);
      break;

   case 4:
      printf("Enter Dividend: ");
      scanf("%d", &num1);
      printf("Enter Divisor: ");
      scanf("%d", &num2);

     //while loop checks for divisor whether it is zero or not
     while(num2 == 0)
     {
        printf("\nDivisor cannot be zero."
               "\nEnter divisor once again: ");
        scanf("%d", &num2);
     }
     x = num1 / num2;
     printf("\nQuotient = %ld", x);
     break;

   case 5:
      printf("Enter any number: \n");
      scanf("%ld", &num1);
      x = num1 * num1;
      printf("Square = %ld", x);
      break;

   case 6:
   return;

   default: printf("\nError");
   }
}

[adsense1]
Output

C calculator

Explanation

This is simple C program to make a calculator using switch case. We have 5 different cases for selecting mathematical operation.

while loop is used to check whether the user selects the option or not.

There is a special case for the division. If the user enters zero in the divisor, the program asks for another divisor and quotient is displayed.