# Find n terms of Fibonacci type series with given first two terms

Given first two numbers of series, find **n** terms of series with these two numbers. The given series follows the same concept as Fibonacci series, i.e., n-th term is sum of (n-1)-th and (n-2)-th terms.

Examples:

Input: first = 5, sec = 8, n = 5 Output: 5, 8, 13, 21, 34 Input: first = 2, sec = 4, n = 5 Output: 2, 4, 6, 10, 16

**Approach:**

The approach is similar to finding Fibonacci series where the summation of last two terms form the next term. Find the sum of first two given numbers, second number now will serve as first number to find the next term and the sum produced will serve as second number to find the next term. Sum of these two newly formed first and second term will form the next required term.

Below is the implementation program:

## C++

`// C++ program to find n terms of ` `// series from given two numbers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find n terms ` `// of series ` `void` `findSeries(` `int` `num, ` `int` `first, ` `int` `sec) ` `{ ` ` ` ` ` `cout << first << ` `" "` `<< sec << ` `" "` `; ` ` ` `int` `counter = 0, sum; ` ` ` ` ` `// find next (num - 2) terms of series ` ` ` `// as first two terms are already given ` ` ` `while` `(counter < num - 2) { ` ` ` `sum = first + sec; ` ` ` `cout << sum << ` `" "` `; ` ` ` `first = sec; ` ` ` `sec = sum; ` ` ` `counter++; ` ` ` `} ` `} ` ` ` `// Drivers code ` `int` `main() ` `{ ` ` ` ` ` `int` `n = 5, first = 2, sec = 4; ` ` ` `findSeries(n, first, sec); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find n terms of ` `// series from given two numbers ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find n terms ` ` ` `// of series ` ` ` `static` `void` `findSeries(` `int` `num, ` ` ` `int` `first, ` `int` `sec) ` ` ` `{ ` ` ` ` ` `System.out.print(first + ` `" "` ` ` `+ sec + ` `" "` `); ` ` ` `int` `counter = ` `0` `, sum; ` ` ` ` ` `// find next (num - 2) terms ` ` ` `// of series as first two ` ` ` `// terms are already given ` ` ` `while` `(counter < num - ` `2` `) ` ` ` `{ ` ` ` `sum = first + sec; ` ` ` `System.out.print( sum + ` `" "` `); ` ` ` `first = sec; ` ` ` `sec = sum; ` ` ` `counter++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Drivers code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `, first = ` `2` `, sec = ` `4` `; ` ` ` ` ` `findSeries(n, first, sec); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find n terms of ` `# series from given two numbers ` ` ` `# Function to find n terms ` `# of series ` `def` `findSeries(num, first, sec) : ` ` ` `print` `(` `"{} {} "` `.` `format` `(first, sec), ` ` ` `end` `=` `"") ` ` ` `counter ` `=` `0` ` ` ` ` `# find next (num - 2) terms of ` ` ` `# series as first two terms are ` ` ` `# already given ` ` ` `while` `(counter < num ` `-` `2` `): ` ` ` `sum` `=` `first ` `+` `sec ` ` ` `print` `(` `"{} "` `.` `format` `(` `sum` `), end` `=` `"") ` ` ` `first ` `=` `sec ` ` ` `sec ` `=` `sum` ` ` `counter ` `=` `counter ` `+` `1` ` ` `# Drivers code ` `n ` `=` `5` `first ` `=` `2` `sec ` `=` `4` `findSeries(n, first, sec) ` ` ` `# This code is contributed by ` `# Manish Shaw (manishshaw1) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find n terms of ` `// series from given two numbers ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find n terms ` ` ` `// of series ` ` ` `static` `void` `findSeries(` `int` `num, ` ` ` `int` `first, ` `int` `sec) ` ` ` `{ ` ` ` ` ` `Console.Write(first + ` `" "` ` ` `+ sec + ` `" "` `); ` ` ` `int` `counter = 0, sum; ` ` ` ` ` `// find next (num - 2) terms ` ` ` `// of series as first two ` ` ` `// terms are already given ` ` ` `while` `(counter < num - 2) ` ` ` `{ ` ` ` `sum = first + sec; ` ` ` `Console.Write( sum + ` `" "` `); ` ` ` `first = sec; ` ` ` `sec = sum; ` ` ` `counter++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Drivers code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 5, first = 2, sec = 4; ` ` ` ` ` `findSeries(n, first, sec); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find n terms of ` `// series from given two numbers ` ` ` `// Function to find n terms ` `// of series ` `function` `findSeries( ` `$num` `, ` `$first` `, ` `$sec` `) ` `{ ` ` ` ` ` `echo` `$first` `, ` `" "` `, ` `$sec` `, ` `" "` `; ` ` ` `$counter` `= 0; ` `$sum` `; ` ` ` ` ` `// find next (num - 2) terms of series ` ` ` `// as first two terms are already given ` ` ` `while` `(` `$counter` `< ` `$num` `- 2) ` ` ` `{ ` ` ` `$sum` `= ` `$first` `+ ` `$sec` `; ` ` ` `echo` `$sum` `, ` `" "` `; ` ` ` `$first` `= ` `$sec` `; ` ` ` `$sec` `= ` `$sum` `; ` ` ` `$counter` `++; ` ` ` `} ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 5; ` ` ` `$first` `= 2; ` ` ` `$sec` `= 4; ` ` ` `findSeries(` `$n` `, ` `$first` `, ` `$sec` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2 4 6 10 16

Similarly, To find Nth number, generate N terms of series in the above manner and print Nth number.

## Recommended Posts:

- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- Find the sum of n terms of the series 1,8,27,64 ....
- Find the sum of all the terms in the n-th row of the given series
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ..... upto n terms
- Find the sum of series 3, -6, 12, -24 . . . upto N terms
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Find sum of the series 1+22+333+4444+...... upto n terms
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Minimum Fibonacci terms with sum equal to K
- Sum of Fibonacci numbers at even indexes upto N terms
- Deriving the expression of Fibonacci Numbers in terms of golden ratio

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.