# Repeated sum of first N natural numbers

Given two integers N and K, the task is to find the sum of first N natural numbers then update N as the previously calculated sum. Repeat these steps K times and finally print the value of N.

Examples:

Input: N = 2, K = 2
Output: 6
Operation 1: n = sum(n) = sum(2) = 1 + 2 = 3
Operation 2: n = sum(n) = sum(3) = 1 + 2 + 3 = 6

Input: N = 3, K = 2
Output: 21

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Find the sum of first N natural numbers using the formula (N * (N + 1)) / 2 then update N with the calculated sum. Repeat these steps exactly K times and print the final value of N.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the sum of ` `// the first n natural numbers ` `int` `sum(``int` `n) ` `{ ` `    ``int` `sum = (n * (n + 1)) / 2; ` `    ``return` `sum; ` `} ` ` `  `// Function to return the repeated sum ` `int` `repeatedSum(``int` `n, ``int` `k) ` `{ ` ` `  `    ``// Perform the operation exactly k times ` `    ``for` `(``int` `i = 0; i < k; i++) { ` ` `  `        ``// Update n with the sum of ` `        ``// first n natural numbers ` `        ``n = sum(n); ` `    ``} ` ` `  `    ``return` `n; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 2, k = 2; ` ` `  `    ``cout << repeatedSum(n, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GFG ` `{ ` `     `  `    ``// Function to return the sum of  ` `    ``// the first n natural numbers  ` `    ``static` `int` `sum(``int` `n)  ` `    ``{  ` `        ``int` `sum = (n * (n + ``1``)) / ``2``;  ` `        ``return` `sum;  ` `    ``}  ` `     `  `    ``// Function to return the repeated sum  ` `    ``static` `int` `repeatedSum(``int` `n, ``int` `k)  ` `    ``{  ` `     `  `        ``// Perform the operation exactly k times  ` `        ``for` `(``int` `i = ``0``; i < k; i++)  ` `        ``{  ` `     `  `            ``// Update n with the sum of  ` `            ``// first n natural numbers  ` `            ``n = sum(n);  ` `        ``}  ` `        ``return` `n;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `n = ``2``, k = ``2``;  ` `     `  `        ``System.out.println(repeatedSum(n, k));  ` `     `  `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the sum of ` `# the first n natural numbers ` `def` `summ(n): ` `    ``suum ``=` `(n ``*` `(n ``+` `1``)) ``/``/` `2` `    ``return` `suum ` ` `  `# Function to return the repeated sum ` `def` `repeatedSum(n, k): ` ` `  `    ``# Perform the operation exactly k times ` `    ``for` `i ``in` `range``(k): ` ` `  `        ``# Update n with the sum of ` `        ``# first n natural numbers ` `        ``n ``=` `summ(n) ` ` `  `    ``return` `n ` ` `  `# Driver code ` `n ``=` `2` `k ``=` `2` ` `  `print``(repeatedSum(n, k)) ` ` `  `# This code is contributed by Mohit Kumar `

\

## C#

 `// C# implementation of the approach  ` `using` `System; ` `     `  `class` `GFG ` `{ ` `     `  `    ``// Function to return the sum of  ` `    ``// the first n natural numbers  ` `    ``static` `int` `sum(``int` `n)  ` `    ``{  ` `        ``int` `sum = (n * (n + 1)) / 2;  ` `        ``return` `sum;  ` `    ``}  ` `     `  `    ``// Function to return the repeated sum  ` `    ``static` `int` `repeatedSum(``int` `n, ``int` `k)  ` `    ``{  ` `     `  `        ``// Perform the operation exactly k times  ` `        ``for` `(``int` `i = 0; i < k; i++)  ` `        ``{  ` `     `  `            ``// Update n with the sum of  ` `            ``// first n natural numbers  ` `            ``n = sum(n);  ` `        ``}  ` `        ``return` `n;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main (String[] args) ` `    ``{  ` `        ``int` `n = 2, k = 2;  ` `     `  `        ``Console.WriteLine(repeatedSum(n, k));  ` `    ``}  ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```6
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.