Program to Print Fibonacci Series (original) (raw)

Last Updated : 28 May, 2026

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 ...

For n = 10, the Fibonacci Series is: 0 1 1 2 3 5 8 13 21 34.

frame_1

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.

C++ `

#include 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;

}

C

#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;

}

Java

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);
}

}

Python

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)

C#

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);
}

}

JavaScript

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);

`

**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.

frame_2

**Mathematical Representation: The relation can be written as: \sum_{k=0}^{n/2} {n-k \choose k} = F_{n+1},
Where:

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:

The relationship becomes more accurate as the Fibonacci numbers grow larger.

frame_3

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 ...

frame_4

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:

Applications of Fibonacci Numbers:

**Problems based on Fibonacci Number/Series: