Create a Calculator in C Language with Complete Explanation

Table of Contents

Introduction

In this Article, we are going to create a Calculator in C Language. Which is used to do Mathematical Calculations like Addition, Subtraction, Multiplication and Divide the two numbers. This is the Basic Project for C Language Developer to start their Project Journey.

Output – Calculator Project

Project Structure

Here we divide our Code Snippet into Parts to understand how the project structure made,

Part – 1 Including Header File

In the header file of Calculator Project we include the “<stdio.h>”. This header file contains the functions such as “printf()” and “scanf()”. which are essential for input and output operations in C Language .

#include <stdio.h>

Part – 2 Main Function

The mian() is the entry point of the program, where execution starts.

int main() {
    // Function body
    return 0;
}

Part – 3 Declaring Variables

Here we declare a variables to store the operator, two numbers, and the result of the operations.

char operator;
double num1, num2, result;

Here, ‘operator’ is of type ‘char’, while ‘num1’, ‘num2’, ‘result’ are type of ‘double’.

Learn C Language

Part – 4 Prompting User Input

The program prompts the user to enter the operator in First case like the user wants to add, subtract, multiply and divide of the two and also in Second case the user put two numbers which are used to make the operations.

printf("Enter operator (+, -, *, /): ");
scanf("%c", &operator);

printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);

Part – 5 Performing Arithmetic Operations

Here we used ‘switch’ statement which perform the appropriate arithmetic based on the operator entered by the user.

switch(operator) {
    case '+':
        result = num1 + num2;
        break;
    case '-':
        result = num1 - num2;
        break;
    case '*':
        result = num1 * num2;
        break;
    case '/':
        if (num2 != 0)
            result = num1 / num2;
        else {
            printf("Error: Division by zero!\n");
            return 1;
        }
        break;
    default:
        printf("Error: Invalid operator!\n");
        return 1;
}

Part – 6 Handling Division by Zero

Make sure the division by zero will not perform in this task. If the denominator (‘num2’) is zero, an error massage is displayed, and the program exits with an error code (‘1’).

Part – 7 Displaying Result

Finally, the result of the arithmetic operation is displayed using the ‘print()’.

printf("Result: %.2lf\n", result);

Part – 8 Returning Exit Status

The ‘main()’ function returns ‘0’ to indicate successful completion of the program.

return 0;

Complete Codes – Calculator

Here is the Complete Code of Project, Calculator in C Language.

#include <stdio.h>

int main() {
    char operator;
    double num1, num2, result;

    // Prompt user for input
    printf("Enter operator (+, -, *, /): ");
    scanf("%c", &operator);

    // Prompt user for numbers
    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);

    // Perform calculation based on operator
    switch(operator) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            // Check if denominator is not zero
            if (num2 != 0)
                result = num1 / num2;
            else {
                printf("Error: Division by zero!\n");
                return 1; // Exit program with error code
            }
            break;
        default:
            printf("Error: Invalid operator!\n");
            return 1; // Exit program with error code
    }

    // Display result
    printf("Result: %.2lf\n", result);

    return 0;
}

Conclusion

Today we clear the topic of how to create an Calculator in C Language. Which the Mostly first and basic topic of the Project Creation in C Language. In the Articles we easily understand how the all coding parts work to create a calculator.

1 thought on “Create a Calculator in C Language with Complete Explanation”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top