C Program to basic calculator program

A basic Calculator is a program that can able to do basic Mathematical Operations like Addition, Subtraction, Multiplication, and Division. For a better understanding of this C calculator program example, we always recommend you to learn the basic topics of C programming listed below:

What is a calculator and how the calculator works?

In this C program, We help you make a simple Calculator that can able to solve basic Mathematical operations like Addition, Subtraction, Multiplication, and Division. Inside the program, the user has to enter the numbers and select the operation that includes

  • Addition
  • Subtraction
  • Multiplication
  • Division.

Then the calculator takes the input and does the user-selected Mathematical operation and shows the result.

How do 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 operation for the calculation. Then, use a switch case statement for selecting any of the operations like '+', '- ', '*', '/ ' based on the user input. Then the control will the switched to that particular case depending on the user-selected operation.

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. To continue the operations without exiting the calculator a do-while loop is used, asking the user for a choice if he wants to do more operations or not.

If the user selects 'y' or 'Y' then the next operation could be performed otherwise, exit from the calculator. The operations will be performed inside the do including user's choice for next operations until while checks user input a choice NO.

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.

Note: The switch case needs a default case which is taken if nothing else matches the switch case.

ALGORITHM

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

STEP 2: Declare global variables n1, n2, result as type double.

STEP 3: Define a function readOperands() of type void, to read the operands to n1,n2

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

STEP 5: Define variables oper, nxt as type char and tmp as int

STEP 6: Start a do-while loop, do the steps from 6 to 10 until the user wants to quit ie., the user inputs a choice other than 'y' || 'Y'

STEP 7: Reset the tmp to 0, Accept the operator oper from the user as to which operation he has to do.

STEP 8: Use the switch case operation to select the operation which the user input and, call the function readOperands() and do the operation inside that switch statement according to the operator selected. The default case will set tmp =1 for incorrect operator input and print "Entered operation is not available".

STEP 9: If tmp is 0 then print the result using a precision operator in float. [ to specify the number of decimal digits needed].

STEP 10: Ask the user "Do you want to continue? (y/n) " and read the input to nxt. The while checks nxt == 'y' || nxt == 'Y' for true and goes to step 6, continue the operations; but if it checks false then stops execution.

C Source Code

                                          #include <stdio.h>

double n1, n2, result; // Global variables

//user defined function to read operands
void readOperands() {
  n1 = n2 = result = 0; // reset variables to 0
  printf("Enter two operands: ");
  scanf(" %lf %lf", & n1, & n2);
}

void main() {
  int tmp;                       // tmp is used to check operator inputed is correct or not
  char oper, nxt;            /* oper is an operator to be selected, nxt is the next choice of user */
  printf("Simulation of a Basic Calculator\n");
  do {
    tmp =0;
    printf("\nEnter an operator (+, -, *, /): ");       // read operation
    scanf(" %c", & oper);
    switch (oper) 
    {
      case '+':
         readOperands();
         result = n1 + n2; // switch case 1 for addition
         break;
      case '-':
         readOperands();
         result = n1 - n2; // case 2 for subtraction
         break;
      case '*':
         readOperands();
         result = n1 * n2; // case 3 for multiplication
         break;
      case '/':
         readOperands();
         result = n1 / n2; // case 4 for division
         break;
     default:
         tmp = 1;
         printf("\nEntered operation is not available\n\n");    // default switch case for incorrect operator input
    }
    if (tmp == 0)
      printf("\n%5.2f %c %5.2f= %5.2f\n\n", n1, oper, n2, result);   // prints the output of the c program
    printf("Do you want to continue? (y/n) "); //    asking user for more operation on calculator
    scanf(" %c", & nxt);
  } while (nxt == 'y'||nxt == 'Y');
}
                                      

OUTPUT

Simulation of a Basic Calculator

Enter an operator (+, -, *, /): +
Enter two operands: 25 56

25.00 + 56.00= 81.00

Do you want to continue? (y/n) y

Enter an operator (+, -, *, /): *
Enter two operands: 15 15

15.00 * 15.00= 225.00

Do you want to continue? (y/n) y

Enter an operator (+, -, *, /): -
Enter two operands: 23.5 12.3

23.50 - 12.30= 11.20

Do you want to continue? (y/n) y

Enter an operator (+, -, *, /): /
Enter two operands: 125 12

125.00 / 12.00= 10.42

Do you want to continue? (y/n) y

Enter an operator (+, -, *, /): %

Entered operation is not available

Do you want to continue? (y/n) n