Simple Calculator Program in C

This C program implements a basic calculator with operations for addition, subtraction, multiplication, and division.

C Code:


/*
A simple calculator program in C.
*/

#include &ltstdio.h&gt

// Function to add two numbers
double add(double num1, double num2) {
    return num1 + num2;
}

// Function to subtract two numbers
double subtract(double num1, double num2) {
    return num1 - num2;
}

// Function to multiply two numbers
double multiply(double num1, double num2) {
    return num1 * num2;
}

// Function to divide two numbers
double divide(double num1, double num2) {
    if (num2 == 0) {
        printf("Error: Cannot divide by zero\n");
        return 0;
    }
    return num1 / num2;
}

int main() {
    // Example usage
    double num1 = 10.5;
    double num2 = 5.2;
    
    // Perform operations
    double sum = add(num1, num2);
    double difference = subtract(num1, num2);
    double product = multiply(num1, num2);
    double quotient = divide(num1, num2);
    
    // Output results
    printf("Sum: %.2lf\n", sum);
    printf("Difference: %.2lf\n", difference);
    printf("Product: %.2lf\n", product);
    printf("Quotient: %.2lf\n", quotient);
    
    return 0;
}
  

Explanation:

The C code above defines a simple calculator program with the following components:

  • Function Definitions: Functions (add, subtract, multiply, divide) are defined to perform basic arithmetic operations.
  • Function Documentation: Each function is described with comments to explain their purpose and behavior.
  • Error Handling: The divide function checks if the divisor (num2) is zero and prints an error message if division by zero is attempted.
  • Example Usage: Variables num1 and num2 are initialized with example values, and the functions are called to perform addition, subtraction, multiplication, and division operations.
  • Output: The results of each operation (sum, difference, product, quotient) are printed to the console using printf with formatting specifiers (%.2lf for double).

Usage:

To use this calculator program:

  1. Edit the values of num1 and num2 to input different numbers.
  2. Compile the C program using a C compiler (e.g., gcc) and run the executable to perform addition, subtraction, multiplication, and division operations based on the input values.

 

Explanation:

  1. Function Definitions:
    • Functions (add, subtract, multiply, divide) are explained with their roles in performing arithmetic operations in C.
  2. Function Documentation:
    • Each function includes comments to describe their purpose and behavior.
  3. Error Handling:
    • The divide function checks for division by zero and prints an error message if num2 is zero.
  4. Example Usage:
    • Variables num1 and num2 are initialized with example values, and the functions (add, subtract, multiply, divide) are called to demonstrate arithmetic operations.
  5. Output:
    • The results of each operation (sum, difference, product, quotient) are printed to the console using printf with formatting specifiers (%.2lf for double precision).
  6. Usage Instructions:
    • Instructions are provided on how to use the calculator program by editing the input variables, compiling the C program using a C compiler, and running the executable to perform arithmetic calculations.

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 :)