C Program to Add Two Numbers

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

Problem Description:

Write a C program to add two numbers.

Problem Solution

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:

advertisement
advertisement

Method 1: (Using Naive Approach)

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

Program/Source Code

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.

  1. /*
  2.  * C program to add two numbers
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int a,b;
  10.     printf("Enter Two Numbers: ");
  11.  
  12.     //Input Two Numbers
  13.     scanf("%d %d",&a,&b);
  14.     int sum=a+b;
  15.  
  16.     //print sum
  17.     printf("Sum of %d and %d is: %d ",a,b,sum);
  18.  
  19.     return 0;
  20. }
Program Explanation

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).

advertisement
Run Time Testcases

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

advertisement
Method 2: (Using Function)

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

Program/Source Code

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.

  1. /*
  2.  * C program to add two numbers using function
  3.  */
  4.  
  5. #include <stdio.h>
  6. //Declare a function to add two numbers
  7. int add(int a,int b)
  8. {
  9.     //returning addition
  10.     return a+b;
  11. }
  12.  
  13. int main()
  14. {
  15.     int a,b;
  16.     printf("Enter Two Numbers: ");
  17.  
  18.     //Input Two Numbers
  19.     scanf("%d %d",&a,&b);
  20.     int sum=add(a,b);
  21.  
  22.     //print sum
  23.     printf("Addition of %d and %d is: %d ",a,b,sum);
  24.     return 0;
  25. }
Program Explanation

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).

Run Time Testcases

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

Method 3: Addition of Two Numbers in C without using Add Operator

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

Program/Source Code

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.

  1. /*
  2.  * C program to add two numbers without using add operator
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int a,b;
  10.     printf("Enter Two Numbers: ");
  11.  
  12.     //Input Two Numbers
  13.     scanf("%d %d",&a,&b);
  14.     int sum=a;
  15.  
  16.     //Incrementing b to a
  17.     for(int i=0;i<b;i++)
  18.     sum++;
  19.  
  20.     //print sum
  21.     printf("Sum of %d and %d is: %d ",a,b,sum);
  22.     return 0;
  23. }
Program Explanation

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).

Run Time Testcases

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

Method 4: Addition of Float Values in C

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

Program/Source Code

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.

  1. /*
  2.  * C program to add float values using add operator
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     float a,b;
  10.     printf("Enter Two Numbers: ");
  11.  
  12.     //Input Two Numbers
  13.     scanf("%f %f",&a,&b);
  14.     float sum=a+b;
  15.  
  16.     //print sum upto 2 decimal places
  17.     printf("Sum of %0.2f and %0.2f is: %0.2f ",a,b,sum);
  18.  
  19.     return 0;
  20. }
Program Explanation

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).

Run Time Testcases

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

Method 5: Addition of Two Numbers in C without using Third Variable

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

Program/Source Code

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.

  1. /*
  2.  * C program to add two numbers without using third variable
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int a,b;
  10.     printf("Enter Two Numbers: ");
  11.  
  12.     //Input Two Numbers
  13.     scanf("%d %d",&a,&b);
  14.  
  15.     //Storing sum in a instead of declaring third variable
  16.     a = a+b;
  17.  
  18.     //print sum
  19.     printf("Sum is: %d ",a);
  20.  
  21.     return 0;
  22. }
Program Explanation

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).

Run Time Testcases

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”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.