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; } |
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 |
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) |
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 |
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 ?> |
4 6
Recommended Posts:
- Program to find Nth term of series 7, 21, 49, 91, 147, 217, ......
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
- Program to find the Nth term of the series 0, 5, 14, 27, 44, ........
- Program to find the N-th term of series 3, 5, 33, 35, 53…. | Set-2
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
- Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
- Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
- Program to find Nth term of series 0, 9, 22, 39, 60, 85, 114, 147, .....
- Program to find Nth term of series 1, 6, 17, 34, 56, 86, 121, 162, .......
- Program to find Nth term of series 3, 12, 29, 54, 86, 128, 177, 234, .....
- Program to find Nth term of series 0, 11, 28, 51, 79, 115, 156, 203, ....
- Program to find Nth term of series 0, 7, 18, 33, 51, 75, 102, 133, .....
- Program to find Nth term of series 0, 10, 30, 60, 99, 150, 210, 280...........
- Program to find Nth term of series 2, 12, 28, 50, 77, 112, 152, 198, .....
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.


