16 May 2024

Fibonacci series

FIBONACCI SERIES  


What is the Fibonacci Series?

The Fibonacci series starts with two numbers: 0 and 1. Each subsequent number in the series is the sum of the two preceding numbers. Mathematically, it is represented as:

F(n)=F(n1)+F(n2)F(n) = F(n-1) + F(n-2)

Where:

  • F(0)=0F(0) = 0
  • F(1)=1F(1) = 1

The sequence begins like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

How to Generate the Fibonacci Series?

To generate the Fibonacci series, you only need to add the last two numbers repeatedly. Here’s a quick example:

  1. Start with 0 and 1.
  2. Add 0+1=10 + 1 = 1 → Sequence: 0, 1, 1
  3. Add 1+1=21 + 1 = 2 → Sequence: 0, 1, 1, 2
  4. Add 1+2=31 + 2 = 3 → Sequence: 0, 1, 1, 2, 3

And so on!


import java.util.*;


class HelloWorld {

    public static void main(String[] args) {

        int n=sc.nextInt();

        int a=0,b=1;

        System.out.print(a+" "+b+" ");

        for(int i=2;i<n;i++){

            int temp=a+b;

            a=b;

            b=temp;

            System.out.print(temp+" ");

        }

    }

}

No comments:

Post a Comment