Program to Print Fibonacci Series (original) (raw)

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

Fibonacci Series

How to Find the Nth term of Fibonacci Series?

In mathematical terms, the number at the **n th position can be represented by:

**Fn = Fn-1 + Fn-2

where, **F 0 = 0 and F 1 = 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 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);

`

Output

0 1 1 2 3 5 8 13 21

Complexity Analysis

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.

Fibonacci-Pascal-relation

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,

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:Fibonacci-Spiral

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:

Applications of Fibonacci Number/Series: