C++ Program To Find Sum of First N Natural Numbers Last Updated : 02 Aug, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Try it on GfG Practice 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 current value of sum for each iteration.sum = sum + xAfter the loop, the variable sum will hold the sum of the first n natural numbers.C++ Program to Find the Sum of Natural Numbers C++ // C++ program to find sum of first // n natural numbers. #include <iostream> using namespace std; // Returns sum of first n natural // numbers int findSum(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + i; return sum; } // Driver code int main() { int n = 5; cout << findSum(n); return 0; } Output15Complexity AnalysisTime Complexity: O(n)Auxiliary Space: O(1) Refer to the complete article Program to find the sum of first n natural numbers for optimized methods to find the sum of N natural numbers. Comment More infoAdvertise with us Next Article Program to find Sum of the series 1*3 + 3*5 + .... K kartik Follow Improve Article Tags : C++ Programs C++ C++ Basic Programs Practice Tags : CPP Similar Reads C++ Program For Sum of Natural Numbers Using Recursion 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: 1 1 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 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 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 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 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 Like