use recursion to print the sum of all numbers upto a given input
Mohammed
Guys, does anyone know the answer?
get use recursion to print the sum of all numbers upto a given input from screen.
C Program to Find the Sum of Natural Numbers using Recursion
In this C programming example, you will learn to find the sum of natural numbers using recursion.
C Program to Find the Sum of Natural Numbers using Recursion
In this example, you will learn to find the sum of natural numbers using a recursive function.
To understand this example, you should have the knowledge of the following C programming topics:
C User-defined functions
C Recursion
The positive numbers 1, 2, 3... are known as natural numbers. The program below takes a positive integer from the user and calculates the sum up to the given number.
Visit this page to find the sum of natural numbers using a loop.
Sum of Natural Numbers Using Recursion
#includeint addNumbers(int n);
int main() { int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0; }
int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else return n; } Run Code
OutputEnter a positive integer: 20
Sum = 210
Suppose the user entered 20.
Initially, addNumbers() is called from main() with 20 passed as an argument.
The number 20 is added to the result of addNumbers(19).
In the next function call from addNumbers() to addNumbers(), 19 is passed which is added to the result of addNumbers(18). This process continues until n is equal to 0.
When n is equal to 0, there is no recursive call. This returns the sum of integers ultimately to the main() function.
Sum of natural numbers using recursion
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Sum of natural numbers using recursion
Difficulty Level : Easy
Last Updated : 16 Jun, 2022
Read Discuss(1) Courses Practice Video
Given a number n, find sum of first natural numbers. To calculate the sum, we will use a recursive function recur_sum().
Examples :Input : 3 Output : 6
Explanation : 1 + 2 + 3 = 6
Input : 5 Output : 15
Explanation : 1 + 2 + 3 + 4 + 5 = 15
Recommended: Please try your approach on first, before moving on to the solution.
Below is code to find the sum of natural numbers up to n using recursion :
C++
// C++ program to find the
// sum of natural numbers up
// to n using recursion
#includeusing namespace std;
// Returns sum of first
// n natural numbers
int recurSum(int n) { if (n <= 1) return n;
return n + recurSum(n - 1);
} // Driver code int main() { int n = 5;
cout << recurSum(n);
return 0; }
Java
Python
C#
PHP
Java Python C# PHP Javascript
Output :15
Time complexity : O(n)Auxiliary space : O(n)To solve this question , iterative approach is the best approach because it takes constant or O(1) auxiliary space and the time complexity will be same O(n).
How to Find the Sum of Natural Numbers Using Recursion
Looking to better understand recursion? Learn how to find the sum of the first n natural numbers.
How to Find the Sum of Natural Numbers Using Recursion
By Yuvraj Chandra
Published Jun 22, 2021
Looking to better understand recursion? Learn how to find the sum of the first n natural numbers.
@=img=@#=img=#
Readers like you help support MUO. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.
Recursion is a process in which a function calls itself directly or indirectly. Recursive algorithms are widely used in computer science to solve complex problems by breaking them down into simpler ones.
You can better understand recursive concepts by solving basic programming problems like the "product of two numbers", "the sum of first n natural numbers", and more.
In this article, you'll learn how to find the sum of the first n natural numbers using recursion.
Problem Statement
You're given a natural number n, you need to find the sum of the first n natural numbers using recursion.
Example 1: Let n = 5Therefore, the sum of the first 5 natural numbers = 1 + 2 + 3 + 4 + 5 = 15.
Thus, the output is 15.
Example 2: Let n = 7Therefore, the sum of the first 7 natural numbers = 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.
Thus, the output is 28.
Example 3: Let n = 6Therefore, the sum of the first 6 natural numbers = 1 + 2 + 3 + 4 + 5 + 6 = 21.
Thus, the output is 21.
Recursive Function to Find the Sum of First N Natural Numbers
Most recursive functions have the following relative structure:
FUNCTION name IF condition THEN RETURN result ELSE CALL FUNCTION name END FUNCTION
To find the sum of the first n natural numbers, observe and apply the following pseudocode:
findSum(n): IF n<=1 THEN RETURN n ELSE
RETURN n + findSum(n-1)
END FUNCTION
Now, you can implement this pseudocode in your favorite programming language.
RELATED:
What Is A Function In Programming?
Note: You can also find the sum of the first n natural numbers using the following mathematical formula:Sum of n natural numbers = n * (n + 1) / 2
Using this method you can find the sum in one step without using recursion.
C++ Implementation to Find the Sum of First N Natural Numbers Using Recursion
Below is the C++ implementation to find the sum of the first n natural numbers using recursion:
// C++ implementation to find the sum of
// first n natural numbers using recursion
#includeusing namespace std;
// Recursive function to find the sum of first n natural numbers
int findSum(int n) { if (n<=1) { return n; } else {
return n + findSum(n-1);
} } // Driver code int main() {
int n1 = 5, n2 = 7, n3 = 6;
cout << "n1: " << n1 << endl;
cout << "n2: " << n2 << endl;
cout << "n3: " << n3 << endl;
cout << "Sum of first " << n1 << " natural numbers: " << findSum(n1) << endl;
cout << "Sum of first " << n2 << " natural numbers: " << findSum(n2) << endl;
cout << "Sum of first " << n3 << " natural numbers: " << findSum(n3) << endl;
return 0; } Output: n1: 5 n2: 7 n3: 6
Sum of first 5 natural numbers: 15
Sum of first 7 natural numbers: 28
Sum of first 6 natural numbers: 21
Python Implementation to Find the Sum of First N Natural Numbers Using Recursion
Below is the Python implementation to find the sum of the first n natural numbers using recursion:
RELATED:
Dynamic Programming: Examples, Common Problems, And Solutions
# Python implementation to find the sum of
# first n natural numbers using recursion
# Recursive function to find the sum of first n natural numbers
def findSum(n): if n<=1: return n else:
return n + findSum(n-1)
# Driver Code n1 = 5 n2 = 7 n3 = 6 print("n1: ", n1) print("n2: ", n2) print("n3: ", n3)
print("Sum of first ", n1, " natural numbers: ", findSum(n1))
print("Sum of first ", n2, " natural numbers: ", findSum(n2))
print("Sum of first ", n3, " natural numbers: ", findSum(n3))
Output: n1: 5 n2: 7 n3: 6
Sum of first 5 natural numbers: 15
Sum of first 7 natural numbers: 28
Sum of first 6 natural numbers: 21
C Implementation to Find the Sum of First N Natural Numbers Using Recursion
Below is the C implementation to find the sum of the first n natural numbers using recursion:
// C implementation to find the sum of
// first n natural numbers using recursion
#include// Recursive function to find the sum of first n natural numbers
int findSum(int n) { if (n<=1) { return n; } else {
return n + findSum(n-1);
} } // Driver code int main() {
int n1 = 5, n2 = 7, n3 = 6;
printf("n1: %d \n", n1);
printf("n2: %d \n", n2);
printf("n3: %d \n", n3);
printf("Sum of first %d natural numbers: %d \n", n1, findSum(n1));
printf("Sum of first %d natural numbers: %d \n", n2, findSum(n2));
printf("Sum of first %d natural numbers: %d \n", n3, findSum(n3));
Guys, does anyone know the answer?