Sum of natural numbers using recursion

Given a number n, find sum of first n 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
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 #include <iostream> using 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; } |
chevron_right
filter_none
Java
// Java program to find the // sum of natural numbers up // to n using recursion import java.util.*; import java.lang.*; class GFG { // Returns sum of first // n natural numbers public static int recurSum(int n) { if (n <= 1) return n; return n + recurSum(n - 1); } // Driver code public static void main(String args[]) { int n = 5; System.out.println(recurSum(n)); } } // This code is contributed by Sachin Bisht |
chevron_right
filter_none
Python
# Python code to find sum # of natural numbers upto # n using recursion # Returns sum of first # n natural numbers def recurSum(n): if n <= 1: return n return n + recurSum(n - 1) # Driver code n = 5print(recurSum(n)) |
chevron_right
filter_none
C#
// C# program to find the // sum of natural numbers // up to n using recursion using System; class GFG { // Returns sum of first // n natural numbers public static int recurSum(int n) { if (n <= 1) return n; return n + recurSum(n - 1); } // Driver code public static void Main() { int n = 5; Console.WriteLine(recurSum(n)); } } // This code is contributed by vt_m |
chevron_right
filter_none
PHP
<?php // PHP program to find the // sum of natural numbers // up to n using recursion // Returns sum of first // n natural numbers function recurSum($n) { if ($n <= 1) return $n; return $n + recurSum($n - 1); } // Driver code $n = 5; echo(recurSum($n)); // This code is contributed by Ajit. ?> |
chevron_right
filter_none
Output :
15
Recommended Posts:
- Product of 2 numbers using recursion | Set 2
- Product of 2 Numbers using Recursion
- Sum of cubes of first n odd natural numbers
- Find m-th summation of first n natural numbers.
- Find the average of first N natural numbers
- Program to find sum of first n natural numbers
- Difference between sum of the squares of first n natural numbers and square of sum
- Print all increasing sequences of length k from first n natural numbers
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Recursion
- Tail Recursion
- Sum of the series 1^1 + 2^2 + 3^3 + ..... + n^n using recursion
- Find the value of ln(N!) using Recursion
- Mean of array using recursion
- Solving f(n)= (1) + (2*3) + (4*5*6) ... n using Recursion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t



