Given a number n, find the n-th term in the series 1, 3, 6, 10, 15, 21…

Examples:

Input : 3 Output : 6 Input : 4 Output : 10

The given series represent triangular numbers which are sums of natural numbers.

**Naive approach :**

The series basically represents sums of natural numbers. First term is sum of single number. Second term is sum of two numbers, and so on. A simple solution is to add the first n natural numbers.

## C++

`// CPP program to find n-th term of ` `// series 1, 3, 6, 10, 15, 21... ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to find the nth term of series ` `int` `term(` `int` `n) ` `{ ` ` ` `// Loop to add numbers ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `ans += i; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `cout << term(n) ; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find n-th term of ` `// series 1, 3, 6, 10, 15, 21... ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find the nth term of series ` ` ` `static` `int` `term(` `int` `n) ` ` ` `{ ` ` ` `// Loop to add numbers ` ` ` `int` `ans = ` `0` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `ans += i; ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `System.out.println(term(n)); ` ` ` `} ` `} ` ` ` ` ` `// This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find ` `# n-th term of ` `# series 1, 3, 6, 10, 15, 21... ` ` ` ` ` `# Function to find the ` `# nth term of series ` `def` `term(n) : ` ` ` `# Loop to add numbers ` ` ` `ans ` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `,n` `+` `1` `) : ` ` ` `ans ` `=` `ans ` `+` `i ` ` ` ` ` `return` `ans ` ` ` ` ` `# Driver code ` `n ` `=` `4` `print` `(term(n)) ` ` ` `# This code is contributed ` `# by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find n-th term of ` `// series 1, 3, 6, 10, 15, 21... ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find the nth term ` ` ` `// of series ` ` ` `static` `int` `term(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Loop to add numbers ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `ans += i; ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4; ` ` ` ` ` `Console.WriteLine(term(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find n-th term of ` `// series 1, 3, 6, 10, 15, 21... ` ` ` `// Function to find the nth ` `// term of series ` `function` `term(` `$n` `) ` `{ ` ` ` ` ` `// Loop to add numbers ` ` ` `$ans` `= 0; ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `$ans` `+= ` `$i` `; ` ` ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver code ` `$n` `= 4; ` `echo` `(term(` `$n` `)) ; ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

10

Time Complexity of this solution is O(n).

#### Efficient approach :

The pattern in this series is **n ^{th} term is equal to sum of (n-1)^{th} term and n**.

Example :

n = 22^{nd}term equals to sum of 1^{st}term and 2 i.e A2 = A1 + 2 = 1 + 2 = 3 Similarly, A3 = A2 + 3 = 3 + 3 = 6 and so on..

We get:

A(n) = A(n - 1) + n = A(n - 2) + n + (n - 1) = A(n - 3) + n + (n - 1) + (n - 2) . . . = A(1) + 2 + 3... + (n-1) + n A(n) = 1 + 2 + 3 + 4... + (n - 1) + n = n(n + 1) / 2 i.e A(n) is sum of First n natural numbers.

Below is the implementation of the above approach:

## C++

`// CPP program to find the n-th ` `// term in series 1 3 6 10 ... ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find nth term ` `int` `term(` `int` `n) ` `{ ` ` ` `return` `n * (n + 1) / 2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `cout << term(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the n-th ` `// term in series 1 3 6 10 ... ` `import` `java.io.*; ` ` ` `class` `Series { ` ` ` ` ` `// Function to find nth term ` ` ` `static` `int` `term(` `int` `n){ ` ` ` `return` `n * (n + ` `1` `) / ` `2` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `4` `; ` ` ` `System.out.println(term(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Chinmoy Lenka ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find the Nth ` `# term in series 1 3 6 10 ... ` ` ` `# Function to print nth term ` `# of series 1 3 6 10 .... ` `def` `term(n): ` ` ` `return` `n ` `*` `(n ` `+` `1` `) ` `/` `2` ` ` `# Driver code ` `n ` `=` `4` `print` `term(n) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the n-th ` `// term in series 1 3 6 10 ... ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find nth term ` ` ` `static` `int` `term(` `int` `n) ` ` ` `{ ` ` ` `return` `n * (n + 1) / 2; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4; ` ` ` ` ` `Console.WriteLine(term(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the n-th ` `// term in series 1 3 6 10 ... ` ` ` `// Function to find nth term ` `function` `term(` `$n` `) ` `{ ` ` ` `return` `$n` `* (` `$n` `+ 1) / 2; ` `} ` ` ` `// Driver code ` `$n` `= 4; ` `echo` `(term(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

10

**Time Complexity:** O(1)

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.

## Recommended Posts:

- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- First term from given Nth term of the equation F(N) = (2 * F(N - 1)) % 10^9 + 7
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Program to find Nth term in the given Series
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to find Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,...
- Program to find Nth term of series 9, 23, 45, 75, 113...
- Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
- Find Nth term of series 1, 4, 15, 72, 420...
- Find Nth term of the series 5, 13, 25, 41, 61...
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
- Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
- Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
- Find Nth term of the series 3, 14, 39, 84...
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...

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.