The Fibonacci Series is a sequence of numbers in which each number is the sum of the two previous numbers. The first two numbers are: 0, 1. So, the sequence becomes: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...
- It is not just important in mathematics, but also appears in art, architecture, and computer science.
- The Fibonacci Series can be represented mathematically as: Fn = Fn−1 + Fn−2 , where, F0 = 0 and F1 = 1.
For n = 10, the Fibonacci Series is: 0 1 1 2 3 5 8 13 21 34.

Given a number n, The task is to print first n terms of the Fibonacci Series.
Examples:
Input: n = 5
Output: 0 1 1 2 3
Explanation: The Fibonacci Series starts with 0 and 1.
3rd term = 0 + 1 = 1
4th term = 1 + 1 = 2
5th term = 1 + 2 = 3
So, the series becomes: 0 1 1 2 3
Input: n = 1
Output: 0
Explanation: For n = 1, only the first Fibonacci number is printed.
Using Iteration - O(n) Time O(1) Space
The idea is to use a loop and keep track of the previous two Fibonacci numbers. Each next Fibonacci number is obtained by adding the previous two numbers.
#include <iostream>
using namespace std;
// Function to print Fibonacci Series
void printFib(int n)
{
// Handle invalid input
if (n < 1)
{
cout << "Invalid Number of terms";
return;
}
// Initialize first two Fibonacci numbers
int prev2 = 0;
int prev1 = 1;
// Print first Fibonacci number
cout << prev2 << " ";
// If n is 1, stop here
if (n == 1)
{
return;
}
// Print second Fibonacci number
cout << prev1 << " ";
// Generate Fibonacci numbers from 3rd term
for (int i = 3; i <= n; i++)
{
int curr = prev1 + prev2;
cout << curr << " ";
// Update previous numbers
prev2 = prev1;
prev1 = curr;
}
}
// Driver Code
int main()
{
int n = 5;
printFib(n);
return 0;
}
#include <stdio.h>
// Function to print Fibonacci Series
void printFib(int n)
{
// Handle invalid input
if (n < 1)
{
printf("Invalid Number of terms");
return;
}
// Initialize first two Fibonacci numbers
int prev2 = 0;
int prev1 = 1;
// Print first Fibonacci number
printf("%d ", prev2);
// If n is 1, stop here
if (n == 1)
{
return;
}
// Print second Fibonacci number
printf("%d ", prev1);
// Generate Fibonacci numbers from 3rd term
for (int i = 3; i <= n; i++)
{
int curr = prev1 + prev2;
printf("%d ", curr);
// Update previous numbers
prev2 = prev1;
prev1 = curr;
}
}
// Driver Code
int main()
{
int n = 5;
printFib(n);
return 0;
}
public class GfG {
// Function to print Fibonacci Series
static void printFib(int n)
{
// Handle invalid input
if (n < 1)
{
System.out.println("Invalid Number of terms");
return;
}
// Initialize first two Fibonacci numbers
int prev2 = 0;
int prev1 = 1;
// Print first Fibonacci number
System.out.print(prev2 + " ");
// If n is 1, stop here
if (n == 1)
{
return;
}
// Print second Fibonacci number
System.out.print(prev1 + " ");
// Generate Fibonacci numbers from 3rd term
for (int i = 3; i <= n; i++)
{
int curr = prev1 + prev2;
System.out.print(curr + " ");
// Update previous numbers
prev2 = prev1;
prev1 = curr;
}
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
printFib(n);
}
}
def printFib(n):
# Handle invalid input
if n < 1:
print("Invalid Number of terms")
return
# Initialize first two Fibonacci numbers
prev2 = 0
prev1 = 1
# Print first Fibonacci number
print(prev2, end=" ")
# If n is 1, stop here
if n == 1:
return
# Print second Fibonacci number
print(prev1, end=" ")
# Generate Fibonacci numbers from 3rd term
for i in range(3, n + 1):
curr = prev1 + prev2
print(curr, end=" ")
# Update previous numbers
prev2 = prev1
prev1 = curr
# Driver Code
if __name__ == "__main__":
n = 5
printFib(n)
using System;
class GfG {
// Function to print Fibonacci Series
static void printFib(int n)
{
// Handle invalid input
if (n < 1) {
Console.WriteLine("Invalid Number of terms");
return;
}
// Initialize first two Fibonacci numbers
int prev2 = 0;
int prev1 = 1;
// Print first Fibonacci number
Console.Write(prev2 + " ");
// If n is 1, stop here
if (n == 1) {
return;
}
// Print second Fibonacci number
Console.Write(prev1 + " ");
// Generate Fibonacci numbers from 3rd term
for (int i = 3; i <= n; i++) {
int curr = prev1 + prev2;
Console.Write(curr + " ");
// Update previous numbers
prev2 = prev1;
prev1 = curr;
}
}
// Driver Code
static void Main()
{
int n = 5;
printFib(n);
}
}
function printFib(n) {
// Handle invalid input
if (n < 1) {
console.log('Invalid Number of terms');
return;
}
// Initialize first two Fibonacci numbers
let prev2 = 0;
let prev1 = 1;
// Print first Fibonacci number
process.stdout.write(prev2 +'');
// If n is 1, stop here
if (n == 1) {
return;
}
// Print second Fibonacci number
process.stdout.write(prev1 +'');
// Generate Fibonacci numbers from 3rd term
for (let i = 3; i <= n; i++) {
let curr = prev1 + prev2;
process.stdout.write(curr +'');
// Update previous numbers
prev2 = prev1;
prev1 = curr;
}
}
// Driver Code
let n = 5;
printFib(n);
Output
0 1 1 2 3
Time Complexity: O(n)
Space Complexity: O(1)
Pascal’s Triangle
Pascal’s Triangle is a triangular arrangement of numbers where each number is the sum of the two numbers directly above it. It is widely used in combinatorics, probability, and binomial expansion. An interesting relation exists between Pascal’s Triangle and the Fibonacci Sequence. The sum of the shallow diagonals of Pascal’s Triangle forms the Fibonacci sequence.
For example:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Diagonal sums:
1
1
1 + 1 = 2
1 + 2 = 3
1 + 3 + 1 = 5
So, the sequence becomes: 1 1 2 3 5 ... , which is the Fibonacci sequence.

Mathematical Representation: The relation can be written as:
Where:
- Fn+ is the (n+1)th Fibonacci number
{n-k \choose k} represents binomial coefficients from Pascal’s Triangle
The Fibonacci sequence can be generated by adding the shallow diagonals of Pascal’s Triangle. This shows a beautiful mathematical connection between combinatorics and number sequences.
Golden Ratio
The Golden Ratio, often denoted by the Greek letter phi (Φ or ϕ), is a special mathematical constant that has been studied by mathematicians, scientists, artists, and architects for centuries.
It is an irrational number, meaning its decimal representation never ends and never repeats. Its approximate value is: ϕ ≈ 1.6180339887
The ratio of consecutive Fibonacci numbers approaches the Golden Ratio as we move further in the Fibonacci sequence.
The following image shows how the division of consecutive Fibonacci numbers forms the Golden Ratio:
- x-axis: F(n+1)/F(n), where F(n) represents a Fibonacci number
- y-axis: Represents the value of the obtained ratio
The relationship becomes more accurate as the Fibonacci numbers grow larger.

The ratio of successive Fibonacci numbers approximates the Golden Ratio, and this relationship becomes more accurate as you move further along the Fibonacci sequence.
The Fibonacci Spiral
The Fibonacci Spiral is formed by drawing quarter circles inside squares whose side lengths follow the Fibonacci sequence. The side lengths of the squares are: 1, 1, 2, 3, 5, 8, 13 ...

The resulting spiral is known as the Fibonacci Spiral or 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.
Some Facts about Fibonacci Numbers:
- Fibonacci numbers were first mentioned in Indian mathematics by Pingala while studying patterns in Sanskrit poetry.
- The sequence was later named after the Italian mathematician Leonardo of Pisa, also known as Fibonacci.
- The sequence is also used in art and architecture, including structures like the Parthenon.
- November 23 is celebrated as Fibonacci Day because
11/23forms the first four Fibonacci numbers:1, 1, 2, 3. - The family tree of honey bees also follows the Fibonacci sequence.
Applications of Fibonacci Numbers:
- Mile to kilometer approximation : If a distance in miles is a Fibonacci number then the succeeding Fibonacci number is its kilometer representation. Example: If we take a number from Fibonacci series i.e., 13 then the kilometer 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.
Problems based on Fibonacci Number/Series:
- Sum of Fibonacci Numbers
- How to check if a given number is Fibonacci number?
- Program to print first n Fibonacci Numbers
- Program to print Fibonacci Triangle
- Minimum Fibonacci terms with sum equal to K
- Largest subset whose all elements are Fibonacci numbers
- Tiling Problem
- Fibonacci Search
- Matrix Exponentiation
- Count Possible Decodings of a given Digit Sequence
- Count number of binary strings without consecutive 1’s
- Find nth Fibonacci number using Golden ratio
- Maximum games played by winner
- Count possible ways to construct buildings
- Find two Fibonacci numbers whose sum can be represented as N
- Minimum number of Fibonacci jumps to reach end