C Program to Perform Addition, Subtraction, Multiplication, and Division

Howdy readers, today you will learn how to write a program to perform addition, subtraction, multiplication, and division of two numbers using the C Programming language.

C Program to Perform Addition, Subtraction, Multiplication, and Division

This program prompts the user to enter two numbers, then it perform the following operations:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division

So, without any delay, let’s begin this tutorial.

C Program to Perform Addition, Subtraction, Multiplication and Division

C Program

// C Program to Perform Addition, Subtraction, Multiplication and Division
#include <stdio.h>
int main(){
    int num1, num2;
    float sum, diff, prod, div;
    
    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);
    
    // Adding two numbers
    sum = num1 + num2;
    
    // Subtracting two numbers
    diff = num1 - num2;
    
    // Multiplying two numbers
    prod = num1 * num2;
    
    // Dividing two numbers
    div = num1 / num2;
    
    // Displaying result
    printf("Sum of two numbers is: %.2f\n", sum);
    printf("Difference of two numbers is: %.2f\n", diff);
    printf("Product of two numbers is: %.2f\n", prod);
    printf("Division of two numbers is: %.2f", div);
    
    return 0;
}

Output

Enter the first number: 30
Enter the second number: 9
Sum of two numbers is: 39.00
Difference of two numbers is: 21.00
Product of two numbers is: 270.00
Division of two numbers is: 3.00
C Program to Perform Addition, Subtraction, Multiplication, and Division

Explanation

    int num1, num2;
    float sum, diff, prod, div;

In the above program, we have declared two int data types and four float data type variables named num1, num2, sum, diff, prod, and div respectively.

    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);

Now, the user is asked to enter two numbers. These numbers get stored in the num1 and num2 named variables.

    // Adding two numbers
    sum = num1 + num2;
    
    // Subtracting two numbers
    diff = num1 - num2;
    
    // Multiplying two numbers
    prod = num1 * num2;
    
    // Dividing two numbers
    div = num1 / num2;

We perform the addition, subtraction, multiplication, and division of two numbers using their corresponding arithmetic operators.

    // Displaying result
    printf("Sum of two numbers is: %.2f\n", sum);
    printf("Difference of two numbers is: %.2f\n", diff);
    printf("Product of two numbers is: %.2f\n", prod);
    printf("Division of two numbers is: %.2f", div);

The result is displayed on the screen using printf() function. Here, \n is a newline character, and %.2f format specifier is used to limit the result to 2 decimal places.

Conclusion

I hope after reading this post, you understand how to write a program to perform addition, subtraction, multiplication, and division using the C Programming language.

If you have any queries regarding the tutorial, then let us know in the comment section. We will be glad to solve all of your queries.

Leave a Comment