C Program to basic calculator program

For a better understanding of this C calculator program example, we always recommend you to learn the basic topics of C programming listed below:

This tutorial presents a learning exercise to help you make a simple calculator program in C. In C language, you can create a simple calculator, displaying the different arithmetical operations. Mainly the operations are

  • Addition
  • Subtraction
  • Multiplication
  • Division.

So to make a Calculator we need a choice to select the Operation and do that selected Operation to get the result.

How we implement a calculator using the C program?

It is a C program that is used for performing mathematical calculations such as addition, subtraction, multiplication, and division. The calculator C program accepts two numbers from the user and also the mathematical operator for the calculation. After the operation, we have to display the result. For implementing a calculator program in C, we are using a switch statement for selecting any of the operations based on the user input. Basic switch statements have cases to check which case user needs. Here we use '+', '- ', '*', '/ ', also switch needs a default case which is taken if nothing else matches the switch case. The syntax of the switch case is given below.


switch (n)
  {
     case constant 1:
           // code to be executed if n is equal to constant1;
     break;
     case constant 2:
           // code to be executed if n is equal to constant2;
     break;
     .
     .
     default:
          // code to be executed if n doesn't match any constant
  }
 

In the above code, the break statement is used to prevent the code from running into the next case. We are also using the precision operator to do the float point numbers as we need to display only a certain extent of decimal places.

ALGORITHM 

STEP 1: Import the header libraries into the C program to use the built-in functions like printf and scanf.

STEP 2: Start the program execution by using the main() Function.

STEP 3: Define operation as character and numbers and result as float datatype.

STEP 4: Accept the numbers from the user using printf and scanf built-in functions and save that in variables.

STEP 5: Accept the operator from the user as to what operation he has to do.

STEP 6: Use the switch case operation to select the operation which the user input and do the operation inside that switch statement.

STEP 7: Print the result using float datatype using a precision operator in float. [ to specify the number of decimal digits needed].

C Source Code

                                          #include <stdio.h>

void main() {
  char oper; /* oper is an operator to be selected */
  double n1, n2, result;
  printf("Simulation of a Simple Calculator\n\n"); //  /n for next line
 
   printf("Enter an operator (+, -, *, /): ");
  scanf("%c", & oper);
  printf("Enter two operands: ");
  scanf("%lf %lf", & n1, & n2);
  switch (oper) {
  case '+':
    result = n1 + n2; // switch case 1 for addition
    break;
  case '-':
    result = n1 - n2; // case 2 for subtraction
    break;
  case '*':
    result = n1 * n2;
    break;
  case '/':
    result = n1 / n2;
    break;
  default:
    printf("Error in operation\n"); // default switch case error in operation
    break;
  }
  printf("\n%5.2f %c %5.2f= %5.2f\n", n1, oper, n2, result); // prints the output of the c program
}
                                      

OUTPUT

Simulation of Simple Calculator

Enter the operator [+,-,*,/]+
Enter two numbers
3 5

3.00 +  5.00=  8.00

RUN2

Simulation of Simple Calculator

Enter the operator [+,-,*,/]-
Enter two numbers
12.75
8.45

12.75 -  8.45=  4.30

RUN3

Simulation of Simple Calculator

Enter the operator [+,-,*,/]*
Enter two numbers
12 12

12.00 * 12.00= 144.00


RUN4

Simulation of Simple Calculator

Enter the operator [+,-,*,/]/
Enter two numbers
5
9

 5.00 /  9.00=  0.56