C Program to basic calculator program


January 8, 2023, Learn eTutorial
1301

Calculator is a program that can able to do basic Mathematical Operations like Addition, Subtraction, Multiplication, and Division.

What is a calculator and how the calculator works?

In this C program, We help you make a simple Calculator, for that 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?

After getting the input from the user for operands and operator, we use a switch case statement for selecting any of the operations like '+',  '- ',  '*', and '/ ' based on the user input. Then the control will the switched to that particular case.

We are also using the precision operator for float point numbers as we need to display only a certain extent of decimal places. For continuing the calculator without exiting, a do-while loop is used. Then ask 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 the user's choice for the next operations until while checks user input a choice NO.

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 n1, n2, and 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: Declare oper, nxt as type character and tmp as integer

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.


To make a calculator program, we used the below concepts in C programming, we recommend you to refer those for better understanding

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