Header-C
Header-C

 

Introduction:

In this tutorial, we will learn how to create a simple calculator program in the C programming language. The calculator will be able to perform basic arithmetic operations like addition, subtraction, multiplication, and division. This project serves as a great way for beginners to practice control structures, functions, and handling user input in C.

Objective:

The objective of this project is to create a C program that asks the user for two numbers and an operation to perform (add, subtract, multiply, or divide), and then prints the result of that operation. The program will be interactive, allowing users to input their data and get instant results. Additionally, we will include error handling for invalid input and division by zero.

Code:

#include 

void add(float a, float b) {
    printf("Result: %.2f\n", a + b);
}

void subtract(float a, float b) {
    printf("Result: %.2f\n", a - b);
}

void multiply(float a, float b) {
    printf("Result: %.2f\n", a * b);
}

void divide(float a, float b) {
    if (b != 0) {
        printf("Result: %.2f\n", a / b);
    } else {
        printf("Error! Division by zero is not allowed.\n");
    }
}

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

    // Ask for user input
    printf("Enter first number: ");
    scanf("%f", &num1);

    printf("Enter an operator (+, -, *, /): ");
    scanf(" %c", &operator);  // The space before %c is to consume any leading whitespace characters

    printf("Enter second number: ");
    scanf("%f", &num2);

    // Perform the operation based on the operator entered
    switch (operator) {
        case '+':
            add(num1, num2);
            break;
        case '-':
            subtract(num1, num2);
            break;
        case '*':
            multiply(num1, num2);
            break;
        case '/':
            divide(num1, num2);
            break;
        default:
            printf("Error! Invalid operator.\n");
    }

    return 0;
}

Explanation of the Program Structure:

The program is structured in the following way:

  1. Functions for Operations:We define four functions for performing the basic arithmetic operations: add, subtract, multiply, and divide. Each function takes two float arguments (the numbers to operate on) and prints the result.
  2. Input Section:The program first asks the user to input two numbers and an operator. It uses scanf to get the input values from the user. The operator is captured as a character (‘+’, ‘-‘, ‘*’, or ‘/’).
  3. Switch Statement for Operation:Based on the operator input by the user, a switch statement is used to choose the corresponding function to perform the operation. If the operator is invalid, an error message is displayed.
  4. Error Handling:We have included error handling for division by zero. If the user attempts to divide by zero, the divide function will print an error message.

How to Run the Program:

Follow these steps to run the program:

    1. Step 1: Open your text editor or Integrated Development Environment (IDE) and create a new C file (e.g., calculator.c).
    2. Step 2: Copy the code provided above into your C file.
    3. Step 3: Save the file and open a terminal or command prompt in the directory where the file is saved.
    4. Step 4: Compile the C program by typing the following command (assuming you’re using GCC compiler):
gcc calculator.c -o calculator
    1. Step 5: Run the compiled program with the following command:
./calculator
  1. Step 6: The program will prompt you for input. Enter the numbers and operator as instructed to get the result.

Example Output:

Enter first number: 5
Enter an operator (+, -, *, /): +
Enter second number: 3
Result: 8.00

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)