Addition of two numbers in C Programming

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

Flowchart To Add Two Numbers
Flowchart To Add Two Numbers

pseudo code for adding two numbers

  1. Start Program
  2. Input two numbers a and b
  3. sum=a+b
  4. Print sum
  5. End Program

Example Addition of two numbers in C

#include
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

Sum of a+b = 40

Addition program in C with user Input

#include
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);
}
Fig: Data Type Promotion in addition

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

Enter First Number
5
Enter Second Number
5.6
Sum of a+b = 10.600000

Addition program in C of Two long numbers

#include
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);
}
Enter First Number
33500
Enter Second Number
5000
Sum of a+b = 38500

C Program to add two float numbers

#include
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);
}
Enter First Number
12.5
Enter Second Number
12.6
Sum of a+b = 25.1

C Program to add two double numbers

#include
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);
}
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

#include
#include
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

Addition of two numbers in C using command line arguments
Fig: Addition of two numbers in C using command line arguments

Here function atoi() is used to convert string to integer value

Sum of a+b = 10

Addition of two numbers in C using function

#include
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;
}
Enter First Number
5
Enter Second Number
5
Sum of a+b = 10

We discussed addition of two numbers in C Programming.

Read More

Break And Continue Statement In C With Example

Array Of Pointers In C Programming