Given a number **N**, the task is to print the first **N **terms of the following series:

1, 3, 4, 8, 15, 27, 50…

**Examples:**

Input:N = 7Output:1, 3, 4, 8, 15, 27, 50Input:N = 3Output:1, 3, 4

**Approach:** From the given series we can find the formula for Nth term:

1st term = 1, 2nd term = 3, 3rd term = 4

4th term = 1st term + 2nd term + 3rd term

5th term = 2nd term + 3rd term + 4th term

6th term = 3rd term + 4th term + 5th term

.

.

so on

Therefore, the idea is to keep track of the last three terms of the series and find the consecutive terms of the series.

Below is the implementation of above approach:

## C++

`// C++ implementation to print the ` `// N terms of the series whose three ` `// terms are given ` ` ` `#include "bits/stdc++.h" ` `using` `namespace` `std; ` ` ` `// Function to print the series ` `void` `printSeries(` `int` `n, ` `int` `a, ` ` ` `int` `b, ` `int` `c) ` `{ ` ` ` ` ` `int` `d; ` ` ` ` ` `// Generate the ith term and ` ` ` `// print it ` ` ` `if` `(n == 1) { ` ` ` `cout << a << ` `" "` `; ` ` ` `return` `; ` ` ` `} ` ` ` `if` `(n == 2) { ` ` ` `cout << a << ` `" "` `<< b << ` `" "` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `cout << a << ` `" "` `<< b ` ` ` `<< ` `" "` `<< c << ` `" "` `; ` ` ` ` ` `for` `(` `int` `i = 4; i <= n; i++) { ` ` ` `d = a + b + c; ` ` ` `cout << d << ` `" "` `; ` ` ` `a = b; ` ` ` `b = c; ` ` ` `c = d; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 7, a = 1, b = 3; ` ` ` `int` `c = 4; ` ` ` ` ` `// Function Call ` ` ` `printSeries(N, a, b, c); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to print the ` `// N terms of the series whose three ` `// terms are given ` ` ` `//include "bits/stdJava.h" ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function to print the series ` `static` `void` `printSeries(` `int` `n, ` `int` `a, ` ` ` `int` `b, ` `int` `c) ` `{ ` ` ` `int` `d; ` ` ` ` ` `// Generate the ith term and ` ` ` `// print it ` ` ` `if` `(n == ` `1` `) ` ` ` `{ ` ` ` `System.out.print(a + ` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` `if` `(n == ` `2` `) ` ` ` `{ ` ` ` `System.out.print(a + ` `" "` `+ b + ` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `System.out.print(a + ` `" "` `+ ` ` ` `b + ` `" "` `+ ` ` ` `c + ` `" "` `); ` ` ` ` ` `for` `(` `int` `i = ` `4` `; i <= n; i++) ` ` ` `{ ` ` ` `d = a + b + c; ` ` ` `System.out.print(d + ` `" "` `); ` ` ` `a = b; ` ` ` `b = c; ` ` ` `c = d; ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `7` `, a = ` `1` `, b = ` `3` `; ` ` ` `int` `c = ` `4` `; ` ` ` ` ` `// Function Call ` ` ` `printSeries(N, a, b, c); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to print the ` `# N terms of the series whose three ` `# terms are given ` ` ` `# Function to print the series ` `def` `printSeries(n, a, b, c): ` ` ` ` ` `# Generate the ith term and ` ` ` `# print it ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `print` `(a, end ` `=` `" "` `); ` ` ` `return` `; ` ` ` ` ` `if` `(n ` `=` `=` `2` `): ` ` ` `print` `(a, b, end ` `=` `" "` `); ` ` ` `return` `; ` ` ` ` ` `print` `(a, b, c, end ` `=` `" "` `); ` ` ` ` ` `for` `i ` `in` `range` `(` `4` `, n ` `+` `1` `): ` ` ` `d ` `=` `a ` `+` `b ` `+` `c; ` ` ` `print` `(d, end ` `=` `" "` `); ` ` ` `a ` `=` `b; ` ` ` `b ` `=` `c; ` ` ` `c ` `=` `d; ` ` ` `# Driver Code ` `N ` `=` `7` `; a ` `=` `1` `; b ` `=` `3` `; ` `c ` `=` `4` `; ` ` ` `# Function Call ` `printSeries(N, a, b, c); ` ` ` `# This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to print the ` `// N terms of the series whose three ` `// terms are given ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to print the series ` `static` `void` `printSeries(` `int` `n, ` `int` `a, ` ` ` `int` `b, ` `int` `c) ` `{ ` ` ` `int` `d; ` ` ` ` ` `// Generate the ith term and ` ` ` `// print it ` ` ` `if` `(n == 1) ` ` ` `{ ` ` ` `Console.Write(a + ` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` `if` `(n == 2) ` ` ` `{ ` ` ` `Console.Write(a + ` `" "` `+ ` ` ` `b + ` `" "` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `Console.Write(a + ` `" "` `+ ` ` ` `b + ` `" "` `+ ` ` ` `c + ` `" "` `); ` ` ` ` ` `for` `(` `int` `i = 4; i <= n; i++) ` ` ` `{ ` ` ` `d = a + b + c; ` ` ` `Console.Write(d + ` `" "` `); ` ` ` ` ` `a = b; ` ` ` `b = c; ` ` ` `c = d; ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 7, a = 1, b = 3; ` ` ` `int` `c = 4; ` ` ` ` ` `// Function call ` ` ` `printSeries(N, a, b, c); ` `} ` `} ` ` ` `// This code is contributed by rock cool ` |

*chevron_right*

*filter_none*

**Output:**

1 3 4 8 15 27 50

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:

- Program to print the series 2, 15, 41, 80, 132, 197… till N terms
- Program to print the series 1, 9, 17, 33, 49, 73, 97... till N terms
- Program to print triangular number series till n
- Program to print the series 2, 1, 4, 3, 6, 5, .... up to N terms
- Find the Sum of the series 1/2 - 2/3 + 3/4 - 4/5 + ... till N terms
- Find the Sum of the series 1 + 1/3 + 1/5 + 1/7 + ... till N terms
- Find the Sum of the series 1, 2, 3, 6, 9, 18, 27, 54, ... till N terms
- Find the Sum of the series 1 + 2 + 9 + 64 + 625 + 7776 ... till N terms
- Print the first N terms of the series 6, 28, 66, 120, 190, 276, ...
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Find n terms of Fibonacci type series with given first two terms
- Program to print the Sum of series -1 + 2 + 11 + 26 + 47 +.....
- Sum of the series 1, 2, 4, 3, 5, 7, 9, 6, 8, 10, 11, 13.. till N-th term
- Program to print Arithmetic Progression series
- Program to print binomial expansion series
- Sum of the first N terms of the series 5,12, 23, 38....
- Sum of the series 5+55+555+.. up to n terms
- Sum of first n terms of a given series 3, 6, 11, .....
- Sum of the first N terms of the series 2,10, 30, 68,....

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.