C++ Program For Sum of Natural Numbers Using Recursion Last Updated : 23 Jun, 2023 Comments Improve Suggest changes Like Article Like Report Natural numbers include all positive integers from 1 to infinity. It does not include zero (0). Given a number n, find the sum of the first n natural numbers. To calculate the sum, we will use the recursive function recur_sum(). Examples: Input: 3Output: 6Explanation: 1 + 2 + 3 = 6 Input: 5Output: 15Explanation: 1 + 2 + 3 + 4 + 5 = 15 The Sum of Natural Numbers Using Recursion Below is a C++ program 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; } Output15 Comment More infoAdvertise with us Next Article C++ Program For Sum of Natural Numbers Using Recursion kartik Follow Improve Article Tags : C++ Programs C++ C++ Basic Programs Practice Tags : CPP Similar Reads C++ Program To Find Sum of First N Natural Numbers Natural numbers are those positive whole numbers starting from 1 (1, 2, 3, 4, ...). In this article, we will learn to write a C++ program to calculate the sum of the first N natural numbers. AlgorithmInitialize a variable sum = 0.Run a loop from i = 1 to n.Inside the loop, add the value of i to the 1 min read Sum of array Elements without using loops and recursion Given an array of N elements, the task is to find the Sum of N elements without using loops(for, while & doWhile) and recursion.Examples: Input: arr[]={1, 2, 3, 4, 5} Output: 15 Input: arr[]={10, 20, 30} Output: 60 j Approach: Unconditional Jump Statements can be used to solve this problem.Uncon 5 min read C++ Program For Finding Subarray With Given Sum - Set 1 (Nonnegative Numbers) Given an unsorted array of non-negative integers, find a continuous subarray that adds to a given number. Examples : Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Output: Sum found between indexes 2 and 4 Sum of elements between indices 2 and 4 is 20 + 3 + 10 = 33 Input: arr[] = {1, 4, 0, 0, 3, 10, 5 min read C++ Program For Fibonacci Numbers The Fibonacci series is the sequence where each number is the sum of the previous two numbers. The first two numbers of the Fibonacci series are 0 and 1, and they are used to generate the entire series.Examples:Input: 5Output: 5Explanation: As 5 is the 5th Fibonacci number of series 0, 1, 1, 2, 3, 5 5 min read Program to find Sum of the series 1*3 + 3*5 + .... Given a series: Sn = 1*3 + 3*5 + 5*7 + ... It is required to find the sum of first n terms of this series represented by Sn, where n is given taken input.Examples: Input : n = 2 Output : S<sub>n</sub> = 18Explanation:The sum of first 2 terms of Series is1*3 + 3*5= 3 + 15 = 18Input : n = 8 min read C++ 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 = 4 10 min read Program to find N-th term of the series a, b, b, c, c, c,....... Given a number N. The task is to write a program to find the N-th term in the below series: a, b, b, c, c, c, d, d, d, d, ..... Examples: Input : 12 Output : e Input : 288 Output : x The idea is to use AP sum formula to find the solution to this problem. Clearly the series is depicted as 1a, 2b's, 3 8 min read C/C++ program for calling main() in main() Given a number N, the task is to write C/C++ program to print the number from N to 1 by calling the main() function using recursion.Examples: Input: N = 10 Output: 10 9 8 7 6 5 4 3 2 1Input: N = 5 Output: 5 4 3 2 1 Approach:Use static variable to initialise the given number N.Print the number N and 2 min read Sum of first n term of Series 3, 5, 9, 17, 33.... Given n, we need to find sum of first n terms of the series represented as Sn = 3 + 5 + 9 + 17 + 33 ⦠upto nExamples: Input : 2Output : 83 + 5 = 8Input : 5Output : 673 + 5 + 9 + 17 + 33 = 67 Let, the nth term be denoted by tn. This problem can easily be solved by splitting each term as follows : Sn 7 min read TCS Coding Practice Question | Fibonacci Series Given a number 'n', the task is to print the Fibonacci series using Command Line Arguments. The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ........ In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the re 4 min read Like