A Simple Calculator in C language allows you to perform basic arithmetic operations like addition, subtraction, multiplication, and division. The program will ask the user to input two numbers and an operator, then it performs the operation based on the input and displays the result.

Steps to Build a Simple Calculator:

  1. Include Header Files: Include the stdio.h library for input and output functions.
  2. Declare Variables: You need variables to store two numbers, the result, and the operator.
  3. Take Input: Ask the user to enter two numbers and the operator (such as +, -, *, /).
  4. Use Conditional Statements: Use if-else or switch statements to perform the operation based on the user's choice of the operator.
  5. Display the Result: After performing the operation, output the result.

Program:

#include <stdio.h> int main() { char operator; // Operator variable to store +, -, *, or / double num1, num2, result; // Variables to store two numbers and the result // Asking the user to input the operator printf("Enter an operator (+, -, *, /): "); scanf("%c", &operator); // Asking the user to input two numbers printf("Enter two numbers: "); scanf("%lf %lf", &num1, &num2); // Switch case to perform the operation based on the operator switch (operator) { case '+': result = num1 + num2; printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result); break; case '-': result = num1 - num2; printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result); break; case '*': result = num1 * num2; printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result); break; case '/': // Check for division by zero if (num2 != 0) { result = num1 / num2; printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result); } else { printf("Error! Division by zero is not allowed.\n"); } break; default: printf("Error! Operator is not correct.\n"); break; } return 0; }

Explanation:

  1. Variables:

    • char operator: This variable stores the arithmetic operator (+, -, *, or /) entered by the user.
    • double num1, num2: These variables store the two numbers on which the operation will be performed.
    • double result: This variable will store the result of the arithmetic operation.
  2. Input:

    • The program asks the user to input an operator with the statement: printf("Enter an operator (+, -, *, /): ");
    • It also asks for two numbers: printf("Enter two numbers: ");
  3. Switch Statement:

    • The switch statement is used to select the operation based on the operator entered by the user.
    • If the user inputs +, the program performs addition.
    • If the user inputs -, the program performs subtraction, and similarly for multiplication (*) and division (/).
    • For division, it also checks if the second number is 0 to avoid division by zero.
  4. Division by Zero:

    • If the user attempts to divide by zero, the program will print an error message: "Error! Division by zero is not allowed.".
  5. Output:

    • The program uses printf to display the result of the operation.

Sample Output:

Enter an operator (+, -, *, /): + Enter two numbers: 4.5 6.3 4.50 + 6.30 = 10.80
Enter an operator (+, -, *, /): / Enter two numbers: 7 0 Error! Division by zero is not allowed.

Key Points:

  • %lf is used to read double values (floating-point numbers).
  • The switch statement is an efficient way to handle multiple conditions based on the value of operator.
  • Error Handling for division by zero ensures that the program does not crash when invalid input is provided.
  • Result Formatting: The result is displayed with two decimal places using %.2lf for better readability.

This calculator program can be extended further by adding more operations or error checks, but it covers the basic arithmetic functionality efficiently.