# Program to find Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,…

Given a number N. The task is to write a program to find the N-th term in the below series:

0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8,…..

**Examples**:

Input : N = 10 Output : 4 Input : N = 7 Output : 6

On observing carefully, you will find that the series is a mixture of 2 series:

- Terms at odd positions in the given series form the series of even numbers in increasing order starting from 0. Like, 0,2,4,6,..
- Terms at even positions in the given series are derived from the previous term using the formula (previousTerm/2). That is, the terms at even positions are half of their previous term.

Now, it is known that every odd positioned term forms an even series starting from 0 and every even positioned term is the half of the previous odd positioned term.

Therefore first check whether the input number N is even or odd. If it is odd, set N=(N/2) + 1(since there are Two series running parallelly) and find the Nth term by using formula 2*(N-1) ( N-1 because the series starts with 0).

Similarly, if N is even, set N = N/2, use the previous formula and divide the answer by 2.

Below is the implementation of above approach:

## C++

`// CPP program to find N-th term ` `// in the series ` `#include <iostream> ` `#include <math.h> ` `using` `namespace` `std; ` ` ` `// Function to find N-th term ` `// in the series ` `void` `findNthTerm(` `int` `n) ` `{ ` ` ` `// If n is even ` ` ` `if` `(n % 2 == 0) { ` ` ` `n = n / 2; ` ` ` `n = 2 * (n - 1); ` ` ` `cout << n / 2 << endl; ` ` ` `} ` ` ` `// If n is odd ` ` ` `else` `{ ` ` ` `n = (n / 2) + 1; ` ` ` `n = 2 * (n - 1); ` ` ` `cout << n << endl; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `X = 10; ` ` ` `findNthTerm(X); ` ` ` ` ` `X = 7; ` ` ` `findNthTerm(X); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find N-th term ` `// in the series ` ` ` `// Function to find N-th term ` `// in the series ` `class` `GFG ` `{ ` `static` `void` `findNthTerm(` `int` `n) ` `{ ` ` ` `// If n is even ` ` ` `if` `(n % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `n = n / ` `2` `; ` ` ` `n = ` `2` `* (n - ` `1` `); ` ` ` `System.out.println(n / ` `2` `); ` ` ` `} ` ` ` ` ` `// If n is odd ` ` ` `else` ` ` `{ ` ` ` `n = (n / ` `2` `) + ` `1` `; ` ` ` `n = ` `2` `* (n - ` `1` `); ` ` ` `System.out.println(n); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `X = ` `10` `; ` ` ` `findNthTerm(X); ` ` ` ` ` `X = ` `7` `; ` ` ` `findNthTerm(X); ` `} ` `} ` ` ` `// This code is contributed by Subhadeep ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find N-th term ` `# in the series ` ` ` `# Function to find N-th term ` `# in the series ` `def` `findNthTerm(n): ` ` ` ` ` `# If n is even ` ` ` `if` `(n ` `%` `2` `=` `=` `0` `): ` ` ` `n ` `=` `n ` `/` `/` `2` ` ` `n ` `=` `2` `*` `(n ` `-` `1` `) ` ` ` `print` `( n ` `/` `/` `2` `) ` ` ` ` ` `# If n is odd ` ` ` `else` `: ` ` ` `n ` `=` `(n ` `/` `/` `2` `) ` `+` `1` ` ` `n ` `=` `2` `*` `(n ` `-` `1` `) ` ` ` `print` `(n) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `X ` `=` `10` ` ` `findNthTerm(X); ` ` ` ` ` `X ` `=` `7` `; ` ` ` `findNthTerm(X) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find N-th term ` `// in the series ` `using` `System; ` ` ` `// Function to find N-th term ` `// in the series ` `class` `GFG ` `{ ` `static` `void` `findNthTerm(` `int` `n) ` `{ ` ` ` `// If n is even ` ` ` `if` `(n % 2 == 0) ` ` ` `{ ` ` ` `n = n / 2; ` ` ` `n = 2 * (n - 1); ` ` ` `Console.Write(n / 2); ` ` ` `} ` ` ` ` ` `// If n is odd ` ` ` `else` ` ` `{ ` ` ` `n = (n / 2) + 1; ` ` ` `n = 2 * (n - 1); ` ` ` `Console.Write(n); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `X = 10; ` ` ` `findNthTerm(X); ` ` ` `Console.Write(` `"\n"` `); ` ` ` `X = 7; ` ` ` `findNthTerm(X); ` `} ` `} ` ` ` `// This code is contributed ` `// by Smitha ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find N-th ` `// term in the series ` ` ` `// Function to find N-th ` `// term in the series ` `function` `findNthTerm(` `$n` `) ` `{ ` ` ` `// If $n is even ` ` ` `if` `(` `$n` `% 2 == 0) ` ` ` `{ ` ` ` `$n` `= ` `$n` `/ 2; ` ` ` `$n` `= 2 * (` `$n` `- 1); ` ` ` `echo` `$n` `/ 2 . ` `"\n"` `; ` ` ` `} ` ` ` ` ` `// If $n is odd ` ` ` `else` ` ` `{ ` ` ` `$n` `= (int)(` `$n` `/ 2) + 1; ` ` ` `$n` `= 2 * (` `$n` `- 1); ` ` ` `echo` `$n` `. ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `$X` `= 10; ` `findNthTerm(` `$X` `); ` ` ` `$X` `= 7; ` `findNthTerm(` `$X` `); ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4 6

## Recommended Posts:

- Program to find Nth term of series 0, 9, 22, 39, 60, 85, 114, 147, .....
- Program to find Nth term of series 0, 11, 28, 51, 79, 115, 156, 203, ....
- Program to find Nth term of series 2, 12, 28, 50, 77, 112, 152, 198, .....
- Program to find Nth term of series 4, 14, 28, 46, 68, 94, 124, 158, .....
- Program to find Nth term of series 3, 12, 29, 54, 86, 128, 177, 234, .....
- Program to find Nth term of series 1, 6, 17, 34, 56, 86, 121, 162, .......
- Program to find the Nth term of series 5, 12, 21, 32, 45......
- Program to find N-th term of series 3, 5, 33, 35, 53....
- Program to find Nth term of the series 2, 4, 3, 4, 15...
- Program to find the Nth term of the series 0, 5, 14, 27, 44, ........
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find Nth term of series 0, 7, 18, 33, 51, 75, 102, 133, .....
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
- Program to find Nth term of the series 3, 6, 18, 24, ...
- Program to find the N-th term of series 3, 5, 33, 35, 53…. | Set-2

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.