Given two integers **A** and **B** which are the first two terms of the series and another integer **N**. The task is to find the **N ^{th}** number using Fibonacci rule i.e.

**fib(i) = fib(i – 1) + fib(i – 2)**

**Example:**

Input:A = 2, B = 3, N = 4

Output:8

The series will be 2, 3, 5, 8, 13, 21, …

And the 4th element is 8.

Input:A = 5, B = 7, N = 10

Output:343

**Approach:** Intilalize variable **sum = 0** that stores sum of the previous two values. Now, run a loop from **i = 2 to N** and for each index update value of **sum = A + B** and **A = B, B = sum**. Then, finally return the sum which is the required **Nth element**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return the Nth number of ` `// the modified Fibonacci series where ` `// A and B are the first two terms ` `int` `findNthNumber(` `int` `A, ` `int` `B, ` `int` `N) ` `{ ` ` ` ` ` `// To store the current element which ` ` ` `// is the sum of previous two ` ` ` `// elements of the series ` ` ` `int` `sum = 0; ` ` ` ` ` `// This loop will terminate when ` ` ` `// the Nth element is found ` ` ` `for` `(` `int` `i = 2; i < N; i++) { ` ` ` `sum = A + B; ` ` ` ` ` `A = B; ` ` ` ` ` `B = sum; ` ` ` `} ` ` ` ` ` `// Return the Nth element ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `A = 5, B = 7, N = 10; ` ` ` ` ` `cout << findNthNumber(A, B, N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the Nth number of ` ` ` `// the modified Fibonacci series where ` ` ` `// A and B are the first two terms ` ` ` `static` `int` `findNthNumber(` `int` `A, ` `int` `B, ` `int` `N) ` ` ` `{ ` ` ` ` ` `// To store the current element which ` ` ` `// is the sum of previous two ` ` ` `// elements of the series ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// This loop will terminate when ` ` ` `// the Nth element is found ` ` ` `for` `(` `int` `i = ` `2` `; i < N; i++) ` ` ` `{ ` ` ` `sum = A + B; ` ` ` ` ` `A = B; ` ` ` ` ` `B = sum; ` ` ` `} ` ` ` ` ` `// Return the Nth element ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `A = ` `5` `, B = ` `7` `, N = ` `10` `; ` ` ` ` ` `System.out.println(findNthNumber(A, B, N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the Nth number of ` `# the modified Fibonacci series where ` `# A and B are the first two terms ` `def` `findNthNumber(A, B, N): ` ` ` ` ` `# To store the current element which ` ` ` `# is the sum of previous two ` ` ` `# elements of the series ` ` ` `sum` `=` `0` ` ` ` ` `# This loop will terminate when ` ` ` `# the Nth element is found ` ` ` `for` `i ` `in` `range` `(` `2` `, N): ` ` ` `sum` `=` `A ` `+` `B ` ` ` ` ` `A ` `=` `B ` ` ` ` ` `B ` `=` `sum` ` ` ` ` `# Return the Nth element ` ` ` `return` `sum` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `A ` `=` `5` ` ` `B ` `=` `7` ` ` `N ` `=` `10` ` ` ` ` `print` `(findNthNumber(A, B, N)) ` ` ` `# This code is contributed by Ashutosh450 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the Nth number of ` ` ` `// the modified Fibonacci series where ` ` ` `// A and B are the first two terms ` ` ` `static` `int` `findNthNumber(` `int` `A, ` `int` `B, ` `int` `N) ` ` ` `{ ` ` ` ` ` `// To store the current element which ` ` ` `// is the sum of previous two ` ` ` `// elements of the series ` ` ` `int` `sum = 0; ` ` ` ` ` `// This loop will terminate when ` ` ` `// the Nth element is found ` ` ` `for` `(` `int` `i = 2; i < N; i++) ` ` ` `{ ` ` ` `sum = A + B; ` ` ` ` ` `A = B; ` ` ` ` ` `B = sum; ` ` ` `} ` ` ` ` ` `// Return the Nth element ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `A = 5, B = 7, N = 10; ` ` ` ` ` `Console.WriteLine(findNthNumber(A, B, N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

343

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:

- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- C/C++ Program for nth multiple of a number in Fibonacci Series
- Nth term of a Custom Fibonacci series
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Program to find last two digits of Nth Fibonacci number
- Find nth Fibonacci number using Golden ratio
- Program to find Nth odd Fibonacci Number
- Fast Doubling method to find the Nth Fibonacci number
- Factorial of each element in Fibonacci series
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Find the maximum possible value of the minimum value of modified array
- Find the modified array after performing k operations of given type
- Find n terms of Fibonacci type series with given first two terms
- Python | Find fibonacci series upto n using lambda
- G-Fact 18 | Finding nth Fibonacci Number using Golden Ratio
- Nth Even Fibonacci Number
- Nth XOR Fibonacci number
- Nth Fibonacci number using Pell's equation
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....

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.