Program to Print Fibonacci Series
Last Updated :
04 Nov, 2024
Ever wondered about the cool math behind the Fibonacci series? This simple pattern has a remarkable presence in nature, from the arrangement of leaves on plants to the spirals of seashells. We're diving into this Fibonacci Series sequence. It's not just math, it's in art, nature, and more! Let's discover the secrets of the Fibonacci series together.
What is the Fibonacci Series?
The Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers of the Fibonacci series are 0 and 1 and are used to generate the Fibonacci series.
Fibonacci SeriesHow to Find the Nth term of Fibonacci Series?
In mathematical terms, the number at the nth position can be represented by:
Fn = Fn-1 + Fn-2
where, F0 = 0 and F1 = 1.
For example, Fibonacci series 10 terms are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Please refer Different Ways to Find Nth Fibonacci Number for details.
Program to print first N terms of Fibonacci Series:
Given a number n, our task is to print first n terms of the Fibonacci Series.
Input : n = 5
Output : 0 1 1 2 3
Input n = 1
Output : 0
We are simply going to run a loop and inside the loop, we are going to keep track of the previous 2 Fibonacci Numbers.
C++
// C++ Program to print the fibonacci series
// using iteration (loops)
#include <iostream>
using namespace std;
// Function to print fibonacci series
void printFib(int n)
{
if (n < 1)
{
cout << "Invalid Number of terms\n";
return;
}
// When number of terms is greater than 0
int prev1 = 1;
int prev2 = 0;
cout << prev2 << " ";
// If n is 1, then we do not need to
// proceed further
if (n == 1)
return;
cout << prev1 << " ";
// Print 3rd number onwards using
// the recursive formula
for (int i = 3; i <= n; i++)
{
int curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
cout << curr << " ";
}
}
// Driver code
int main()
{
int n = 9;
printFib(n);
return 0;
}
C
#include <stdio.h>
// Function to print fibonacci series
void printFib(int n) {
if (n < 1) {
printf("Invalid Number of terms\n");
return;
}
// When number of terms is greater than 0
int prev1 = 1;
int prev2 = 0;
printf("%d ", prev2);
// If n is 1, then we do not need to
// proceed further
if (n == 1)
return;
printf("%d ", prev1);
// Print 3rd number onwards using
// the recursive formula
for (int i = 3; i <= n; i++) {
int curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
printf("%d ", curr);
}
}
// Driver code
int main() {
int n = 9;
printFib(n);
return 0;
}
Java
class GfG {
// Function to print fibonacci series
static void printFib(int n) {
if (n < 1) {
System.out.println("Invalid Number of terms");
return;
}
// When number of terms is greater than 0
int prev1 = 1;
int prev2 = 0;
System.out.print(prev2 + " ");
// If n is 1, then we do not need to
// proceed further
if (n == 1)
return;
System.out.print(prev1 + " ");
// Print 3rd number onwards using
// the recursive formula
for (int i = 3; i <= n; i++) {
int curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
System.out.print(curr + " ");
}
}
// Driver code
public static void main(String[] args) {
int n = 9;
printFib(n);
}
}
Python
# Function to print fibonacci series
def print_fib(n):
if n < 1:
print("Invalid Number of terms")
return
# When number of terms is greater than 0
prev1 = 1
prev2 = 0
print(prev2, end=" ")
# If n is 1, then we do not need to
# proceed further
if n == 1:
return
print(prev1, end=" ")
# Print 3rd number onwards using
# the recursive formula
for i in range(3, n + 1):
curr = prev1 + prev2
prev2 = prev1
prev1 = curr
print(curr, end=" ")
# Driver code
if __name__ == "__main__":
n = 9
print_fib(n)
C#
using System;
class GfG
{
// Function to print fibonacci series
static void printFib(int n)
{
if (n < 1)
{
Console.WriteLine("Invalid Number of terms");
return;
}
// When number of terms is greater than 0
int prev1 = 1;
int prev2 = 0;
Console.Write(prev2 + " ");
// If n is 1, then we do not need to
// proceed further
if (n == 1)
return;
Console.Write(prev1 + " ");
// Print 3rd number onwards using
// the recursive formula
for (int i = 3; i <= n; i++)
{
int curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
Console.Write(curr + " ");
}
}
// Driver code
static void Main(string[] args)
{
int n = 9;
printFib(n);
}
}
JavaScript
// Function to print fibonacci series
function printFib(n) {
if (n < 1) {
console.log("Invalid Number of terms");
return;
}
// When number of terms is greater than 0
let prev1 = 1;
let prev2 = 0;
process.stdout.write(prev2 + " ");
// If n is 1, then we do not need to
// proceed further
if (n == 1) return;
process.stdout.write(prev1 + " ");
// Print 3rd number onwards using
// the recursive formula
for (let i = 3; i <= n; i++) {
let curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
process.stdout.write(curr + " ");
}
}
// Driver code
let n = 9;
printFib(n);
Output0 1 1 2 3 5 8 13 21
Complexity Analysis
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Relation Between Pascal triangle and Fibonacci numbers:
Pascal’s triangle is the arrangement of the data in triangular form which is used to represent the coefficients of the binomial expansions, i.e. the second row in Pascal’s triangle represents the coefficients in (x+y)2 and so on. In Pascal’s triangle, each number is the sum of the above two numbers. Pascal’s triangle has various applications in probability theory, combinatorics, algebra, and various other branches of mathematics.

As shown in the image the diagonal sum of the pascal's triangle forms a fibonacci sequence.
Mathematically: \Sigma_{k=0}^{\left \lfloor n/2 \right \rfloor} \binom{n-k}{k} = F_{n+1}
where F_{t}
is the t-th term of the Fibonacci sequence.
Golden Ratio:
Definition: The golden ratio, often denoted by the Greek letter phi (Φ) or the mathematical symbol τ (tau), is a special mathematical constant that has been of interest to mathematicians, scientists, artists, and architects for centuries. It is an irrational number, meaning its decimal representation goes on forever without repeating, and it is approximately equal to 1.6180339887...
The below image shows how the division of consecutive Fibonacci number forms a Golden Ratio i.e,
- x-axis : F(n+1)/F(n), where F( ) represents a Fibonacci number.
- y-axis : represents the value of the fraction obtained in x-axis.

The ratio of successive Fibonacci numbers approximates the golden ratio, and this relationship becomes more accurate as you move further along the Fibonacci sequence.
Fibonacci Spiral:
The Fibonacci spiral is created using a series of quarter circles, with radii that correspond to the Fibonacci numbers as shown in below image:
The resulting spiral is known as a "Fibonacci spiral" or a "Golden Spiral" It is often associated with the Golden Ratio, which is an irrational number approximately equal to 1.61803398875. The Fibonacci spiral is considered visually pleasing and can be found in various aspects of art, architecture, and nature due to its aesthetic qualities and mathematical significance.
Problems based on Fibonacci Number/Series:
Some Facts about Fibonacci Numbers:
- The Fibonacci numbers were first mentioned in Indian mathematics in a work by Pingala on enumerating potential patterns of Sanskrit poetry built from syllables of two lengths. The numbers were named after the Italian mathematician Leonardo of Pisa, also known as Fibonacci.
- The Fibonacci sequence and the golden ratio are often found in natural patterns, such as the arrangement of leaves on a stem, the spirals of a pinecone, the seeds in a sunflower, and in art and architecture, like the Parthenon in Athens.
- November 23 is Fibonacci Day as it forms the first 4 digits of fibonacci numbers 11/23.
- Honey bees' family tree follows fibonacci sequence.
Applications of Fibonacci Number/Series:
- Mile to kilometre approximation : If a distance in miles is a fibonacci number then the succeeding fibonacci number is its kilmometer representation. Example: If we take a number from Fibonacci series i.e., 13 then the kilometre value will be 20.9215 by formulae, which is nearly 21 by rounding.
- Financial Analysis: In the field of technical analysis in finance, Fibonacci retracement is a popular tool used to identify potential levels of support and resistance in stock and commodity price charts.
- Art and Design: The golden ratio, which is closely related to Fibonacci numbers, is often used in art and design to create aesthetically pleasing proportions and layouts. It can be seen in architecture, paintings, and sculptures.
- Data Structures and Algorithms: Fibonacci heaps, a type of data structure in computer science, are used in certain algorithms, such as Dijkstra's algorithm, for efficient graph processing.
Similar Reads
How to check if a given number is Fibonacci number? Given a number ânâ, how to check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .. Examples :Input : 8Output : YesInput : 34Output : YesInput : 41Output : NoApproach 1:A simple way is to generate Fibonacci numbers until the generated number
15 min read
Nth Fibonacci Number Given a positive integer n, the task is to find the nth Fibonacci number.The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21Example:Input:
15+ min read
C++ Program For Fibonacci Numbers The Fibonacci series is the sequence where each number is the sum of the previous two numbers. The first two numbers of the Fibonacci series are 0 and 1, and they are used to generate the entire series.Examples:Input: 5Output: 5Explanation: As 5 is the 5th Fibonacci number of series 0, 1, 1, 2, 3, 5
5 min read
Python Program for n-th Fibonacci number In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2With seed values F0 = 0 and F1 = 1.Table of ContentPython Program for n-th Fibonacci number Using Formula Python Program for n-th Fibonacci number Using RecursionPython Program for n-th
6 min read
Interesting Programming facts about Fibonacci numbers We know Fibonacci number, Fn = Fn-1 + Fn-2. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, .... . Here are some interesting facts about Fibonacci number : 1. Pattern in Last digits of Fibonacci numbers : Last digits of first few Fibonacci Numbers ar
15+ min read
Find nth Fibonacci number using Golden ratio Fibonacci series = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ........Different methods to find nth Fibonacci number are already discussed. Another simple way of finding nth Fibonacci number is using golden ratio as Fibonacci numbers maintain approximate golden ratio till infinite. Golden ratio: \varphi ={\fr
6 min read
Fast Doubling method to find the Nth Fibonacci number Given an integer N, the task is to find the N-th Fibonacci numbers.Examples: Input: N = 3 Output: 2 Explanation: F(1) = 1, F(2) = 1 F(3) = F(1) + F(2) = 2 Input: N = 6 Output: 8 Approach: The Matrix Exponentiation Method is already discussed before. The Doubling Method can be seen as an improvement
14 min read
Tail Recursion for Fibonacci Write a tail recursive function for calculating the n-th Fibonacci number. Examples : Input : n = 4 Output : fib(4) = 3 Input : n = 9 Output : fib(9) = 34 Prerequisites : Tail Recursion, Fibonacci numbersA recursive function is tail recursive when the recursive call is the last thing executed by the
4 min read
Sum of Fibonacci Numbers Given a number positive number n, find value of f0 + f1 + f2 + .... + fn where fi indicates i'th Fibonacci number. Remember that f0 = 0, f1 = 1, f2 = 1, f3 = 2, f4 = 3, f5 = 5, ... Examples : Input : n = 3Output : 4Explanation : 0 + 1 + 1 + 2 = 4Input : n = 4Output : 7Explanation : 0 + 1 + 1 + 2 + 3
9 min read
Fibonacci Series
Program to Print Fibonacci SeriesEver wondered about the cool math behind the Fibonacci series? This simple pattern has a remarkable presence in nature, from the arrangement of leaves on plants to the spirals of seashells. We're diving into this Fibonacci Series sequence. It's not just math, it's in art, nature, and more! Let's dis
8 min read
Program to Print Fibonacci Series in JavaThe Fibonacci series is a series of elements where the previous two elements are added to generate the next term. It starts with 0 and 1, for example, 0, 1, 1, 2, 3, and so on. We can mathematically represent it in the form of a function to generate the n'th Fibonacci number because it follows a con
5 min read
Print the Fibonacci sequence - PythonTo print the Fibonacci sequence in Python, we need to generate a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. The Fibonacci sequence follows a specific pattern that begins with 0 and 1, and every subsequent number is the sum of the two previous num
5 min read
C Program to Print Fibonacci SeriesThe Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers are 0 and 1 which are used to generate the whole series.ExampleInput: n = 5Output: 0 1 1 2 3Explanation: The first 5 terms of the Fibonacci series are 0, 1, 1, 2, 3.In
4 min read
JavaScript Program to print Fibonacci SeriesThe Fibonacci sequence is the integer sequence where the first two terms are 0 and 1. After that, the next term is defined as the sum of the previous two terms. The recurrence relation defines the sequence Fn of Fibonacci numbers:Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1Examples:Input : 5
4 min read
Length of longest subsequence of Fibonacci Numbers in an ArrayGiven an array arr containing non-negative integers, the task is to print the length of the longest subsequence of Fibonacci numbers in this array.Examples: Input: arr[] = { 3, 4, 11, 2, 9, 21 } Output: 3 Here, the subsequence is {3, 2, 21} and hence the answer is 3.Input: arr[] = { 6, 4, 10, 13, 9,
5 min read
Last digit of sum of numbers in the given range in the Fibonacci seriesGiven two non-negative integers M, N which signifies the range [M, N] where M ? N, the task is to find the last digit of the sum of FM + FM+1... + FN where FK is the Kth Fibonacci number in the Fibonacci series. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... Examples: Input: M = 3, N = 9 Output:
5 min read
K- Fibonacci seriesGiven integers 'K' and 'N', the task is to find the Nth term of the K-Fibonacci series. In K - Fibonacci series, the first 'K' terms will be '1' and after that every ith term of the series will be the sum of previous 'K' elements in the same series. Examples: Input: N = 4, K = 2 Output: 3 The K-Fibo
7 min read
Fibonacci Series in BashPrerequisite: Fibonacci Series Write a program to print the Fibonacci sequence up to nth digit using Bash. Examples: Input : 5 Output : Fibonacci Series is : 0 1 1 2 3 Input :4 Output : Fibonacci Series is : 0 1 1 2 The Fibonacci numbers are the numbers in the following integer sequence . 0, 1, 1, 2
1 min read
R Program to Print the Fibonacci SequenceThe Fibonacci sequence is a series of numbers in which each number (known as a Fibonacci number) is the sum of the two preceding ones. The sequence starts with 0 and 1, and then each subsequent number is the sum of the two previous numbers. The Fibonacci sequence has many applications in various fie
2 min read