Simple Calculator Using C Language

Simple Calculator Using C Language

In this article, we will create a simple calculator using C Programming. In the past post, we have created different types of C projects. Are you new to programming and want to try out your skills in C? A simple calculator is the Best project to start with. It helps you practice basic concepts like input, output, and conditional statements in c All languages have the same concept if you know the concept you can write this code in any language. In this article, we will guide you on how to create a simple calculator using C.

What Does Our Calculator Do?

Our calculator will perform basic arithmetic operations like:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)

The user will enter two numbers and choose an operation. The program will then display the result.

Source code of Simple Calculator Using C Language

// Calculator.c
#include <stdio.h>

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

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

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

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

    return 0;
}

Congratulations! You’ve just created a simple calculator in C. This project introduced you to basic concepts like variables, input/output, conditional statements, and arithmetic operations. With practice, you can expand this program to include more advanced features like error handling for invalid inputs or support for more complex calculations.

Happy coding!

Notes: If you have any Kind of problem on the project Contact us on Social Media.