Table of Contents
ToggleIn 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:
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:
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
When b < 0
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!