TeachingBee

Best Ways For Addition Of Two Numbers in C

addition of two numbers in c

In this post we will look into different ways in which we can perform addition of two numbers in C.

In this first program we will take user input two integers and display sum of these integers.In alternative way we will perform this task using user defined function

Addition Of Two Numbers in C

Given two integers. Write a program to display the sum of these integers.

Example:
Input: 10 20
Output: 30

Method 1

Algorithm:

  • Declare three variables, num1 and num2 to store user input and sum variable to store the sum.
  • Now scan any two number entered by user.
  • To read the input numbers we are using scanf() function
  • Store the addition of num1 and num2 to sum variable
  • Print the value of sum as output using printf() function to display the sum of these number
  • The %d used in scanf() and printf() functions is the format specifier which is used for integer data types in C programming.
//Addition Of Two Numbers in C

#include <stdio.h>

int main() {

  int num1, num2, sum;

  // Take two integers from users as input
  printf("Enter first number: ");
  scanf("%d", & num1);
  printf("Enter second number: ");
  scanf("%d", & num2);

  // Store sum to variable
  sum = num1 + num2;

  // Display the result to screen
  printf("Sum of the entered numbers: %d", sum);

  return 0;
}

Output:

Screenshot 2022 02 28 at 3.30.01 PM
Addition of two numbers in C

Method 2: Using Functions

In this method we will try to make above code modular by writing the addition logic in the user defined function sum() and calling this function from the main function. This function takes two integer as parameters and return sum as output.Benefits of this approach is it makes code more readable and reusable. Read more about c functions here.

//Addition Of Two Numbers in C

#include <stdio.h>

/* User defined function sum that takes two integer as parameters. 
   The function adds these numbers and return the result of addition.
 */

int addition(int a, int b) {
  return a + b;
}

int main() {

  int num1, num2, sum;

  // Take two integers from users as input
  printf("Enter first number: ");
  scanf("%d", & num1);
  printf("Enter second number: ");
  scanf("%d", & num2);

  //Calling the function
  sum = addition(num1, num2);

  // Display the result to screen
  printf("Sum of the entered numbers: %d", sum);

  return 0;
}

Output:

Screenshot 2022 02 28 at 3.30.58 PM

Method3: Using Recursion

In this method we will use recursion to find sum of two integers.

  • The sum of two integers a+b can be written as a+1+1+1....(b times)
  • The idea is to add one two first integer b times.
  • The base condition will be to return first integer if b = 0
  • Thus, addition(a,b) = 1+addition(a, b-1) if b>0
  • For the case of negative second integer(b) we need to add -1 instead of 1 and increment b rather than decrement.
  • Thus, addition(a,b) = -1+addition(a, b+1) if b<0
//Addition Of Two Numbers in C

#include<stdio.h>

// Recursive function to calculate sum of two integers.
int addition(int a, int b) {
  // Base case
  if (b == 0) {
    return a;
  } else if (b < 0) {
  // Case when b is negative
    return (-1 + addition(a, b + 1));
  }
  return (1 + addition(a, b - 1));
}

int main() {

int num1, num2, sum;

// Take two integers from users as input
printf("Enter any two number: ");
scanf("%d%d", &num1, &num2);

//Calling the function
sum = addition(num1, num2);

// Display the result to screen
printf("\nSum = %d", sum);

return 0;
}

Output:

When b > 0

Screenshot 2022 02 28 at 3.43.45 PM

When b < 0

Screenshot 2022 02 28 at 3.43.17 PM
Handling negative integers

Conclusion

This was all about addition of two numbers in C. We discussed three ways to perform the addition.The recursion method is space expensive as it uses stack internally to perform addition and is not recommended.

Got a question or just want to chat? Drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Subtraction of Two Numbers Program in C

In this blog post, we will checkout the Subtraction of Two Numbers Program in C. How to Write Subtraction Program in C? To create a C program to subtract two

Add Two Numbers In C

In this blog post, we will checkout the how to add two numbers in C. Algorithm to Add Two Numbers in C To create a C program to add two

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In