Given three integers **A**, **B** and **N**. A Custom Fibonacci series is defined as **F(x) = F(x – 1) + F(x + 1)** where F(1) = A and F(2) = B. Now the task is to find the **N ^{th}** term of this series.

**Examples:**

Input:A = 10, B = 17, N = 3

Output:7

10, 17, 7, -10, -17, …

Input:A = 50, B = 12, N = 10

Output:-50

**Approach:** It can be observed that the series will go on like **A, B, B – A, -A, -B, A – B, A, B, B – A, …**

Below is the implementation of the above approach:

## C++

`// C++ implementation of the Custom Fibonacci series ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the nth term ` `// of the required sequence ` `int` `nth_term(` `int` `a, ` `int` `b, ` `int` `n) ` `{ ` ` ` `int` `z = 0; ` ` ` `if` `(n % 6 == 1) ` ` ` `z = a; ` ` ` `else` `if` `(n % 6 == 2) ` ` ` `z = b; ` ` ` `else` `if` `(n % 6 == 3) ` ` ` `z = b - a; ` ` ` `else` `if` `(n % 6 == 4) ` ` ` `z = -a; ` ` ` `else` `if` `(n % 6 == 5) ` ` ` `z = -b; ` ` ` `if` `(n % 6 == 0) ` ` ` `z = -(b - a); ` ` ` `return` `z; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 10, b = 17, n = 3; ` ` ` ` ` `cout << nth_term(a, b, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the ` `// Custom Fibonacci series ` `class` `GFG ` `{ ` ` ` `// Function to return the nth term ` `// of the required sequence ` `static` `int` `nth_term(` `int` `a, ` `int` `b, ` `int` `n) ` `{ ` ` ` `int` `z = ` `0` `; ` ` ` `if` `(n % ` `6` `== ` `1` `) ` ` ` `z = a; ` ` ` `else` `if` `(n % ` `6` `== ` `2` `) ` ` ` `z = b; ` ` ` `else` `if` `(n % ` `6` `== ` `3` `) ` ` ` `z = b - a; ` ` ` `else` `if` `(n % ` `6` `== ` `4` `) ` ` ` `z = -a; ` ` ` `else` `if` `(n % ` `6` `== ` `5` `) ` ` ` `z = -b; ` ` ` `if` `(n % ` `6` `== ` `0` `) ` ` ` `z = -(b - a); ` ` ` `return` `z; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a = ` `10` `, b = ` `17` `, n = ` `3` `; ` ` ` ` ` `System.out.println(nth_term(a, b, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of the ` `# Custom Fibonacci series ` ` ` `# Function to return the nth term ` `# of the required sequence ` `def` `nth_term(a, b, n): ` ` ` `z ` `=` `0` ` ` `if` `(n ` `%` `6` `=` `=` `1` `): ` ` ` `z ` `=` `a ` ` ` `elif` `(n ` `%` `6` `=` `=` `2` `): ` ` ` `z ` `=` `b ` ` ` `elif` `(n ` `%` `6` `=` `=` `3` `): ` ` ` `z ` `=` `b ` `-` `a ` ` ` `elif` `(n ` `%` `6` `=` `=` `4` `): ` ` ` `z ` `=` `-` `a ` ` ` `elif` `(n ` `%` `6` `=` `=` `5` `): ` ` ` `z ` `=` `-` `b ` ` ` `if` `(n ` `%` `6` `=` `=` `0` `): ` ` ` `z ` `=` `-` `(b ` `-` `a) ` ` ` `return` `z ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `10` ` ` `b ` `=` `17` ` ` `n ` `=` `3` ` ` ` ` `print` `(nth_term(a, b, n)) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the ` `// Custom Fibonacci series ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the nth term ` `// of the required sequence ` `static` `int` `nth_term(` `int` `a, ` `int` `b, ` `int` `n) ` `{ ` ` ` `int` `z = 0; ` ` ` `if` `(n % 6 == 1) ` ` ` `z = a; ` ` ` `else` `if` `(n % 6 == 2) ` ` ` `z = b; ` ` ` `else` `if` `(n % 6 == 3) ` ` ` `z = b - a; ` ` ` `else` `if` `(n % 6 == 4) ` ` ` `z = -a; ` ` ` `else` `if` `(n % 6 == 5) ` ` ` `z = -b; ` ` ` `if` `(n % 6 == 0) ` ` ` `z = -(b - a); ` ` ` `return` `z; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `a = 10, b = 17, n = 3; ` ` ` ` ` `Console.Write(nth_term(a, b, n)); ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

**Output:**

7

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 Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
- 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 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.