Easy way to learn – Arithmetic operators in C

blog img

Here’s a simple example of a C program that performs basic arithmetic operations like addition, subtraction, multiplication, and division, explained line by line:

c

// 1. Preprocessor directive to include standard input-output library functions.
#include <stdio.h>

// 2. Main function that returns an integer (int) and is the entry point for the program.
int main() {        
    
    // 3. Declare variables of type float for storing numbers and results.
    float num1, num2, result;
    
    // 4. Ask the user to input two numbers using printf function.
    printf("Enter two numbers: ");
    
    // 5. Use scanf to get the user's input and store it in num1 and num2.
    scanf("%f %f", &num1, &num2);
    
    // 6. Perform addition and store the result in 'result'.
    result = num1 + num2;
    
    // 7. Print the result of the addition.
    printf("Addition: %.2f\n", result);
    
    // 8. Perform subtraction and print the result.
    result = num1 - num2;
    printf("Subtraction: %.2f\n", result);
    
    // 9. Perform multiplication and print the result.
    result = num1 * num2;
    printf("Multiplication: %.2f\n", result);
    
    // 10. Perform division and print the result. Be sure not to divide by zero.
    if(num2 != 0) {
        result = num1 / num2;
        printf("Division: %.2f\n", result);
    } else {
        printf("Division by zero is not allowed.\n");
    }
    
    return 0;  // 11. Return 0 to indicate successful program execution.
}

Explanation of Each Line:

#include <stdio.h>: This line includes the standard input-output library. It allows the program to use functions like printf (to print) and scanf (to read input).

int main(): The main function is the starting point of every C program. The int return type indicates that the function will return an integer value (which is 0 if the program runs successfully).

float num1, num2, result;: These variables are declared to store two floating-point numbers (num1 and num2) entered by the user, and the result of each arithmetic operation (result).

printf("Enter two numbers: ");: This displays the prompt asking the user to enter two numbers.

scanf("%f %f", &num1, &num2);: This reads the two numbers entered by the user. The %f format specifier is used for reading floating-point numbers, and & is used to give the memory address of the variables num1 and num2 so that their values can be stored.

result = num1 + num2;: This performs the addition of the two numbers and stores the result in the result variable.

printf("Addition: %.2f\n", result);: This prints the result of the addition operation. The format specifier %.2f is used to display the floating-point number with two decimal places.

result = num1 - num2; printf("Subtraction: %.2f\n", result);: This performs subtraction and prints the result. The same process as in the addition is followed.

result = num1 * num2; printf("Multiplication: %.2f\n", result);: This performs multiplication and prints the result.

Division with condition: The division operation is inside an if statement that checks if num2 is not zero. Division by zero is undefined in mathematics, so we avoid that by printing an error message if num2 is zero. If it’s not zero, it performs the division and prints the result.

return 0;: This indicates the successful completion of the program. Returning 0 signals to the operating system that the program executed correctly.

Share your thoughts

Your email address will not be published. All fields are required.

Related post

  1. Easy to read and write the JSON file

    In C, you can handle JSON data by using libraries…

  1. Easy to create a file in C program.

    To create a file in C programming language, you typically…

  1. Easy to creating a Simple calculator in C.

    Here’s a simple calculator program in C that performs basic…

  1. Functions in C – how it’s work

    In C, functions are blocks of code that perform specific…

  1. How to learn constructor – in C

    In C, a constructor is not a built-in feature like…

Popular post

  1. Eclipse IDE – Create New Java Project.

    Opening the New Java Project…

  1. How to start the project in android studio

    Android Studio Open the Android…

  1. How to use ACOSH function in excel

    The ACOSH function returns the…

  1. Complete Header tags in html – easy to learn

    H tags can be used…

  1. Best features in Python programme – easy to learn

    Python is the most widely…