JavaScript Program to Find the Sum of Natural Numbers using Recursion Last Updated : 16 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Finding the sum of natural numbers using recursion involves defining a function that recursively adds numbers from 1 to the given limit. The function repeatedly calls itself with decreasing values until reaching the base case, where it returns the sum. Example: Input: 5Output: 15Explanation: 1 + 2 + 3 + 4 + 5 = 15Input: 10Output: 55Explanation: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55ApproachA simple recursive function for finding the sum of natural numbers repeatedly calls itself with decremented input until reaching the base case. If the input is 1, it returns 1; otherwise, it adds the current input to the result of the function with the input decremented by 1. Example: The function myFunction recursively calculates the sum of natural numbers up to n. It returns 1 if n is 1; otherwise, it adds n to myFunction(n - 1). JavaScript function myFunction(n) { if (n === 1) { return 1; } else { return n + myFunction(n - 1); } } console.log(myFunction(5)); Output15 Time complexity: O(n) Space complexity: O(n) Comment More infoAdvertise with us Next Article JavaScript Program to Find the Sum of Natural Numbers using Recursion P parzival_op Follow Improve Article Tags : JavaScript Web Technologies Similar Reads C# Program to Find Sum of Digits of a Number Using Recursion Given a number, we need to find the sum of digits in the number using recursion. In C#, recursion is a process in which a function calls itself directly or indirectly and the corresponding function is known as a recursive function. It is used to solve problems easily like in this article using recur 2 min read JavaScript Program for Sum of n Terms of Harmonic Progression One can find the sum of n terms of Harmonic Progression using JavaScript. Harmonic Progression is a sequence of real numbers in which each term is reciprocal of Arithmetic Progression. There are different approaches to finding the sum of n terms of Harmonic Progression which are discussed below: Tab 3 min read JavaScript Program for Sum of n terms of Geometric Progression Geometric Progression is a sequence of numbers whose next term is calculated by multiplying the current term with a fixed number known as a common ratio. We are going to learn how we can find the sum of n in terms of Geometric Progression in different ways using JavaScript including iteration, recur 3 min read JavaScript Program for Sum of n Terms of Arithmetic Progression Arithmetic progression is a sequence of numbers in which the difference between any two consecutive terms is always equal. This difference between any two consecutive terms is known as a common difference and it can be positive, negative, or zero. Below are the approaches to find Sum of n terms in A 4 min read Javascript Program To Find The Sum Of Last N Nodes Of The Given Linked List Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list.Examples: Input : 10->6->8->4->12, n = 2 Output : 16 Sum of last two nodes: 12 + 4 = 16 Input : 15->7->9->5->16->14, n = 10 min read Data Structure Design for Two Sum III You have to design a data structure that accepts a stream of integers and then checks if it has a pair of integers that added/sum up to a particular value. Implement the TwoSum class: TwoSum(): Initializes the TwoSum object, with an empty array initially.void add(int number): Adds number to the data 6 min read Recursion in Java In Java, Recursion is a process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using a recursive algorithm, certain problems can be solved quite easily. A few Java recursion examples are Towers of Hanoi (TOH) 6 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read JavaScript Program to Find Sum of Natural Numbers using Recursion We are given a number N and the task is to find the sum of the first n natural numbers using recursion. Approach to Find Sum of Natural Numbers using Recursion:In this approach, we are using the function to get the sum of the first N natural numbers. First, we declare a function findSum(n) and pass 2 min read Java Program to Find Sum of N Numbers Using Recursion Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Assume/Identify the smaller problem from the problem which is similar to the bigger/ori 4 min read Like