C Programming
- Print integer
- Add & subtract
- Odd & even
- Multiply & divide
- Check vowels
- Letter grade program
- Leap year program
- Sum of digits
- Prime numbers
- Sum of numbers
- Find factorial
- Fibonacci series
- Fibonacci triangle
- Number triangle
- Alphabet triangle
- Armstrong numbers
- Palindrome numbers
- Swap number
- Reverse number
- Decimal to binary
- Assembly code in C
- Matrix multiplication
- HCF & LCM
- nCr & nPr
- Print pattern
- Floyd’s triangle
- Pascal triangle
- Count digits
- Strong numbers
- Perfect numbers
- Sum of natural number
- Binary to decimal
- Decimal to octal
- Octal to decimal
- Binary to octal
- Octal to binary
- Add using pointer
- Bubble sort
- Insertion sort
- Selection sort
- Quick sort
- Binary search
- Linear search
- Largest element of array
- Smallest element of array
- Reverse Array
- Variable size & limit
- ASCII value of character
- Sum of array elements
- Number of element in array
- Merge two array
- Insert element in array
- Delete element from array
- Add two matrix
- Transpose matrix
- Print string
- Reverse string
- Delete vowels
- Sort string
- Remove spaces
- Swap string
- Random numbers
- Print date & time
- Print IP address
- Shut down computer
Addition and subtraction of two numbers in C
In this article we will learn different way of addition and subtraction of two numbers using C programming. Actually it is an arithmetic operation of adding or subtracting two number and print their result. For example, if an user gives two integer or float then we have to print the output of their addition and subtraction.
Adding integers in C
// addition and subtraction, adding two integers
#include <stdio.h>
int main (){
int firstNumber, secondNumber, sum;
printf("Enter first number : ");
scanf("%d", &firstNumber);
printf("Enter second number : ");
scanf("%d", &secondNumber);
sum = firstNumber + secondNumber;
printf("\nSum is = %d\n", sum);
return 0;
}
Output of this addition program

Subtracting integer in C
// addition and subtraction, subtracting integer
#include <stdio.h>
int main (){
int number1, number2, result;
printf("Enter first number here : ");
scanf("%d", &number1);
printf("Enter second number here : ");
scanf("%d", &number2);
result = number1 - number2;
printf("\nResult is = %d\n", result);
return 0;
}
Output of this C subtraction program

Adding floating point numbers in C
// adding floating point number, addition and subtraction program
#include <stdio.h>
int main (){
float a, b, result;
printf("Enter first float number here : ");
scanf("%f", &a);
printf("Enter second float number here : ");
scanf("%f", &b);
result = a + b;
printf("\nResult is = %.2f\n", result); // %.2f will print the result with 2 decimal point
return 0;
}
See the output of this program

Subtracting floating point numbers in C
// subtracting floating point number, addition and subtraction program
#include <stdio.h>
int main (){
float x, y, result;
printf("Enter first float number : ");
scanf("%f", &x);
printf("Enter second float number : ");
scanf("%f", &y);
result = x - y;
printf("\nResult is = %.2f\n", result);
return 0;
}
Output of this program

Considering fact before addition and subtraction
Here are some of the most common error we make when adding or subtracting in C. The list is given bellow;
- We should consider the storage capacity for variables. We can not store bigger number than the maximum limit for any variable.
#include <stdio.h>
int main (){
int x, y, sum;
printf("Enter the value of x and y : ");
scanf("%d%d", &x, &y);
sum = x + y;
printf("\nSum is = %d\n", sum);
return 0;
}
This program will give the output correctly like we have given bellow;

But, if a use gives any input that will be more than 2,147,483,647, then we will not get the correct result. See the output bellow;

Here, 2000000000 + 2000000000 = 4000000000
But we have got wrong output because of storage capacity of integer variable is 4 bytes which can store maximum 2,147,483,647.
- Adding floating point number with integer will give wrong result.
#include <stdio.h>
int main (){
int x = 10;
float y = 12.25;
int sum1 = x + y;
printf("\nSum is = %d\n", sum1); // int can not store float number
float sum2 = x + y;
printf("\nSum is = %f\n", sum2); // correct result
printf("\nValue of PI = %d\n", 22/7); // %d can not print float number
return 0;
}
Here we will get wrong result for first and last case. If we use the value of PI is = 22.0/7 or 22/7.0 then we will get correct result. So, here the output will be as following.

Print required output for float number
We can control our output when using floating double variables in C. By default, %lf prints six digits after decimal point. But we can print any number of digits after decimal point using the given method.
// required output for addition and subtraction
#include <stdio.h>
int main (){
float firstNum = 3.125;
float secondNum = 2.454;
float sum = firstNum + secondNum;
printf("%f\n", sum); // by default six digit after decimal point
printf("%.f\n", sum); // print only integer part
printf("%.1f\n", sum); // print 1 digit after decimal point
printf("%.2f\n", sum); // print 2 digit after decimal point
printf("%.3f\n", sum); // print 3 digit after decimal point
printf("%.4f\n", sum); // print 4 digit after decimal point
printf("%.5f\n", sum); // print 5 digit after decimal point
return 0;
}
Output :
5.579000
6
5.6
5.58
5.579
5.5790
5.57900
Process returned 0 (0x0) execution time : 0.026 s
Press any key to continue.
Repeatedly add two numbers in C using loop
#include <stdio.h>
int main(){
int x, y, result;
char ch;
while(1){
printf("\nEnter two integer to add them : ");
scanf("%d%d", &x, &y);
getchar();
result = x + y;
printf("\nResult is = %d\n", result);
printf("\nDo you want to add more (y/n)\n");
scanf("%c", &ch);
if(ch == 'y' || ch == 'Y'){
continue;
}else{
printf("\n\nOk, Thanks\n\n");
break;
}
}
return 0;
}
Output :
Enter two integer to add them : 34 53
Result is = 87
Do you want to add more (y/n)
y
Enter two integer to add them : 60 20
Result is = 80
Do you want to add more (y/n)
n
Ok, Thanks
Process returned 0 (0x0) execution time : 22.940 s
Press any key to continue.
C program to add two numbers using function
#include<stdio.h>
int addition (int a, int b){ // function declaration
int sum = a + b;
return sum;
}
int main(){
int a, b;
printf("Enter two integers : ");
scanf("%d%d", &a, &b);
int result = addition(a,b); // calling function
printf("Result is = %d\n", result);
return 0;
}
Output :
Enter two integers : 10 30
Result is = 40
Process returned 0 (0x0) execution time : 8.253 s
Press any key to continue.
If you have any doubt about work with function then see our function chapter to learn about C function. Now, what you have to do is try to practice these and implement other method to add or subtract integers or floating point numbers
Previous page:Â Print integer in C
Next page:Â Odd and even number program