C Program to Add or Subtract two Integers

Simple-C-Program

In this article, we are going to learn how to Add or Subtract Two Integers in C Language. We will ask the user to enter two integers and then we will learn C Program to Add or Subtract two Integers. Let us learn addition using the C language program first.

C Program to Add Two Integers


In this program, we are asking the user to enter two integers and read their values using the scanf() function, and then once the user enters both numbers, we save them in two variables. These two variables are used to do the addition of both the numbers and the result is saved in a third variable sum. We finally print this sum using printf() function.

  • %d: to take integer input from user,for each integer input we will use %d.
#include <stdio.h>
int main() {    

    int num1, num2, sum;
    
    printf("Please enter two integers: ");

    scanf("%d %d", &num1, &num2);

    sum = num1 + num2;      
    
    printf("The Sum of %d + %d = %d", num1, num2, sum);

    return 0;
}

Output

Please enter two integers: 12 13
The Sum of 12 + 13 = 25

C Program to Substract Two Integers


In this program, we are asking the user to enter two integers numbers, and then once the user enters both numbers, we save them in two variables. These two variables are used to do the subtraction of both the numbers and the result is saved in a third variable diff. We finally print this diff using printf() function.

#include <stdio.h>

int main() {    

    int num1, num2, diff;
    
    printf("Please enter two integers: ");

    scanf("%d %d", &num1, &num2);

    diff = num1 - num2;      
    
    printf("The Difference of %d - %d = %d", num1, num2, diff);
    return 0;
}

Output

Please enter two integers: 25 16
The Difference of 25 - 16 = 9

Conclusion:

In this post, we have learned how to do basic addition and subtraction operations on two integer variables by taking input from user.