**Addition of Two Integers in C:** Addition in math is a **process of combining two or more numbers**. The result of the final answer we get after the process is called the **sum**.

**Examples:**

**Input:** A = 4, B = 3

**Output:** A + B = 4 + 3 = 7

**Input:** A = 12, B = 31

**Output:** A + B = 12 + 31 = 43

Write a C program to add two numbers.

1. Take two numbers as input.

2. Find the sum of two numbers using function and “**+**” operator.

3. Print the final result and exit.

Addition of Two Numbers in C can be found out in following ways:

- Addition of Two Numbers in C using Naive Approach
- Addition of Two Numbers in C using Function
- Addition of Two Numbers in C without using Add Operator
- Addition of Float Values in C
- Addition of Two Numbers in C without using Third Variable

In this approach, we add two integer values using the ‘+‘ operator.

**Example:**

If a=5, b=3, then addition of two numbers is

**sum = a + b** = 5 + 3 = 8

Here is source code of the C program to add two numbers using naive approach. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C program to add two numbers`

`*/`

`#include <stdio.h>`

int main()

`{`

int a,b;

printf("Enter Two Numbers: ");

`//Input Two Numbers`

scanf("%d %d",&a,&b);

int sum=a+b;

`//print sum`

printf("Sum of %d and %d is: %d ",a,b,sum);

return 0;

`}`

1. Take two numbers as input and store it in variable **a** and **b**.

2. Find the sum of two numbers using ‘ **+** ‘ operator. i.e **sum=a+b**

3. Store their addition in the **sum** variable.

4. Print the **sum**.

**Time Complexity: O(1)**

In the above program there are no iterative statements, no function calls, the only operation performed is input output, so time complexity is O(1)

**Space Complexity: O(1)**

Since no auxiliary space is required, the space complexity of adding two numbers is O(1).

**Testcase 1:** In this case, the numbers entered as input to add two numbers are “**5**” and “**3**“.

Enter Two Numbers: 5 3 Sum of 5 and 3 is: 8

**Testcase 2:** In this case, the numbers entered for Add Two Numbers are “**14**” and “**9**” as input.

Enter Two Numbers: 14 9 Sum of 14 and 9 is: 23

In this approach, we add two integer values using a function which returns the addition of two numbers.

**Example:**

If a=7, b=6, then addition of two numbers is

**sum = a + b** = 7 + 6 = 13

Here is source code of the C program to add two numbers using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C program to add two numbers using function`

`*/`

`#include <stdio.h>`

`//Declare a function to add two numbers`

int add(int a,int b)

`{`

`//returning addition`

return a+b;

`}`

int main()

`{`

int a,b;

printf("Enter Two Numbers: ");

`//Input Two Numbers`

scanf("%d %d",&a,&b);

int sum=add(a,b);

`//print sum`

printf("Addition of %d and %d is: %d ",a,b,sum);

return 0;

`}`

1. Take two numbers as input and store it in variable **a** and **b**.

2. Declare a function which takes two numbers as parameters and return their **sum**.

3. Store the value returned by function in the **sum** variable.

4. Print value returned by function as **sum**.

**Time Complexity: O(1)**

In the above program there are no iterative statements, no function calls, the only operation performed is input output, so time complexity is O(1)

**Space Complexity: O(1)**

Since no auxiliary space is required, the space complexity is O(1).

**Testcase 1:** In this case, the numbers entered as input to add two numbers are “**7**” and “**6**“.

Enter Two Numbers: 7 6 Addition of 7 and 6 is: 13

**Testcase 2:** In this case, the numbers entered as input to add two numbers are “**18**” and “**41**“.

Enter Two Numbers: 18 41 Addition of 18 and 41 is: 59

In this approach, we add two integer values using add operator instead we use a for loop to find sum.

Here is source code of the C program to add two numbers without using add operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C program to add two numbers without using add operator`

`*/`

`#include <stdio.h>`

int main()

`{`

int a,b;

printf("Enter Two Numbers: ");

`//Input Two Numbers`

scanf("%d %d",&a,&b);

int sum=a;

`//Incrementing b to a`

for(int i=0;i<b;i++)

`sum++;`

`//print sum`

printf("Sum of %d and %d is: %d ",a,b,sum);

return 0;

`}`

1. Take two numbers as input and store it in variable **a** and **b**.

2. Initiate a for loop to add ‘**b**‘ to ‘**a**‘.

3. Print the **sum** of two variables.

**Time Complexity: O(b)**

As a for loop is executed ‘b‘ times, so time complexity is O(b).

**Space Complexity: O(1)**

Since no auxiliary space is required, the space complexity is O(1).

**Testcase 1:** In this case, the numbers entered as input to add two numbers are “**4**” and “**3**“.

Enter Two Numbers: 4 3 Sum of 4 and 3 is: 7

**Testcase 2:** In this case, the numbers entered as input to add two numbers are “**11**” and “**26**“.

Enter Two Numbers: 11 26 Sum of 11 and 26 is: 37

In this approach, we add two float values using the ‘ + ‘ operator.

**Example:**

If a=1.1, b=2.2, then addition of two numbers is

**sum = a + b** = 1.1 + 2.2 = 3.30

Here is source code of the C program to add float values using add operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C program to add float values using add operator`

`*/`

`#include <stdio.h>`

int main()

`{`

float a,b;

printf("Enter Two Numbers: ");

`//Input Two Numbers`

scanf("%f %f",&a,&b);

float sum=a+b;

`//print sum upto 2 decimal places`

printf("Sum of %0.2f and %0.2f is: %0.2f ",a,b,sum);

return 0;

`}`

1. Take two float numbers as input and store it in variable **a** and **b**.

2. Find the sum of two float numbers using ‘ + ‘ operator. i.e **float sum=a+b;**

3. Store their addition in the **sum** variable (float value).

4. Print the **sum**.

**Time Complexity: O(1)**

In the above program there are no iterative statements, no function calls, the only operation performed is input output, so time complexity is O(1).

**Space Complexity: O(1)**

Since no auxiliary space is required, the space complexity is O(1).

**Testcase 1:** In this case, the numbers entered as input to add two numbers are “**1.1**” and “**2.2**“.

Enter Two Numbers: 1.1 2.2 Sum of 1.10 and 2.20 is: 3.30

**Testcase 2:** In this case, the numbers entered as input to add two numbers are “**4.23**” and “**8.12**“.

Enter Two Numbers: 4.23 8.12 Sum of 4.23 and 8.12 is: 12.35

In this approach, we add two numbers without using any third variable, instead we store the sum in one of the two initial two variables.

**Example:**

If a=9, b=13, then addition of two numbers is

**a + b** = 9 + 13 = 22

Here is source code of the C program to add two numbers without using third variable. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C program to add two numbers without using third variable`

`*/`

`#include <stdio.h>`

int main()

`{`

int a,b;

printf("Enter Two Numbers: ");

`//Input Two Numbers`

scanf("%d %d",&a,&b);

`//Storing sum in a instead of declaring third variable`

a = a+b;

`//print sum`

printf("Sum is: %d ",a);

return 0;

`}`

1. Take two numbers as input and store it in variable **a** and **b**.

2. Find the sum of two numbers using ‘ + ‘ operator. i.e **a = a+b;**

3. Store their addition in the ‘a’ variable instead of declaring a third variable.

4. Print the **sum**.

**Time Complexity: O(1)**

In the above program there are no iterative statements, no function calls, the only operation performed is input output, so time complexity is O(1).

**Space Complexity: O(1)**

Since no auxiliary space is required, the space complexity is O(1).

**Testcase 1:** In this case, the numbers entered as input to add two numbers are “**9**” and “**13**“.

Enter Two Numbers: 9 13 Sum of 9 and 13 is: 22

**Testcase 2:** In this case, the numbers entered as input to add two numbers are “**11**” and “**26**“.

Enter Two Numbers: 11 26 Sum of 11 and 26 is: 37

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

**Related Posts:**

- Watch Advanced C Programming Videos
- Apply for C Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship