Addition of two numbers in C Programming is basic and simple task.
We can create it very easily.
Writing a program is based on Algorithm.
An algorithm is step by step description of program.
So before writing program we must write algorithm to develop this program.
There are different ways to represent an algorithm
1 Flow chart
2. Pseudocode
Flow chart to add two numbers
Pseudocode to add Two Numbers
- Start Program
- Input two numbers a and b
- sum=a+b
- Print sum
- End Program
Example Addition of two numbers in C
1 2 3 4 5 6 7 | #include<stdio.h> int main() { int a=20; int b=20; int sum=a+b; printf("Sum of a+b = %d", sum); } |
here %d is used to print integer value we can also use %i.
Result
1 | Sum of a+b = 40 |
Addition program in C with user Input
1 2 3 4 5 6 7 8 9 10 11 | #include<stdio.h> int main() { int a; float b,sum; printf("\nEnter First Number"); scanf("%d",&a); printf("\nEnter Second Number"); scanf("%f",&b); sum=a+b; printf("\nSum of a+b = %f", sum); } |

Here one variable is int type another is float type.
So int value is automatic promoted to float value
now both are float and its result is float.
This automatic promotion is known as widening.
Output
1 2 3 4 5 | Enter First Number 5 Enter Second Number 5.6 Sum of a+b = 10.600000 |
Addition program in C of Two long numbers
1 2 3 4 5 6 7 8 9 10 | #include<stdio.h> int main() { int a,b; printf("\nEnter First Number"); scanf("%ld",&a); printf("\nEnter Second Number"); scanf("%ld",&b); int sum=a+b; printf("\nSum of a+b = %ld", sum); } |
1 2 3 4 5 | Enter First Number 33500 Enter Second Number 5000 Sum of a+b = 38500 |
C Program to add two float numbers
1 2 3 4 5 6 7 8 9 10 | #include<stdio.h> int main() { float a,b; printf("\nEnter First Number"); scanf("%f",&a); printf("\nEnter Second Number"); scanf("%f",&b); float sum=a+b; printf("\nSum of a+b = %f", sum); } |
1 2 3 4 5 | Enter First Number 12.5 Enter Second Number 12.6 Sum of a+b = 25.1 |
C Program to add two double numbers
1 2 3 4 5 6 7 8 9 10 | #include<stdio.h> int main() { double a,b; printf("\nEnter First Number"); scanf("%lf",&a); printf("\nEnter Second Number"); scanf("%lf",&b); double sum=a+b; printf("\nSum of a+b = %lf", sum); } |
1 2 3 4 5 | Enter First Number 33.5 Enter Second Number 55.2 Sum of a+b = 88.700000 |
Addition of two numbers in C using command line arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[]) { int a,b; if(argc!=3){ printf("Enter two numbers to add"); return -1; } a=atoi(argv[1]); b=atoi(argv[2]); int sum=a+b; printf("\nSum of a+b = %d", sum); return 0; } |
gcc add.c
./a.out 6 4

Here function atoi() is used to convert string to integer value
1 | Sum of a+b = 10 |
Addition of two numbers in C using function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<stdio.h> int add(int a,int b){ return a+b; } int main() { int a,b,sum; printf("\nEnter First Number"); scanf("%d",&a); printf("\nEnter Second Number"); scanf("%d",&b); sum=add(a,b); printf("\nSum of a+b = %d", sum); return 0; } |
1 2 3 4 5 | Enter First Number 5 Enter Second Number 5 Sum of a+b = 10 |
We discussed addition of two numbers in C Programming.