Write a Simple Calculator Program Using C Language



A calculator is a simple tool that helps us to calculate mathematical operations easily and quickly. A basic calculator can perform simple arithmetic operations like subtraction, addition, multiplication, and division.

Begin by writing the C code to create a simple calculator. Then, follow the algorithm given below to write a C program.

Algorithm

Step 1: Declare variables
Step 2: Enter any operator at runtime
Step 3: Enter any two integer values at runtime
Step 4: Apply switch case to select the operator:
        // case '+': result = num1 + num2;
               break;
           case '-': result = num1 - num2;
               break;
           case '*': result = num1 * num2;
               break;
           case '/': result = num1 / num2;
               break;
           default: printf("
Invalid Operator "); Step 5: Print the result

Example: Basic Usage

Following is the C program for the calculator. It specifies the user to enter an operator(+, -, *, /) and two operands. By using the Switch Case it performs the arithmetic operation on the operands and stores the result ?

#include <stdio.h>
int main() {
   char Operator;
   float num1, num2, result = 0;
   printf("
Enter any one operator like +, -, *, / : "); scanf("%c", &Operator); printf("Enter the values of Operands num1 and num2
: "); scanf("%f%f", &num1, &num2); switch (Operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; default: printf("
Invalid Operator "); } printf("The value = %f", result); return 0; }

When the above program is executed, it produces the following result ?

Enter any one operator: +
Enter values of Operands num1 and num2:
23
45
The value = 68.000000
Updated on: 2025-01-21T10:58:52+05:30

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements