Java Programming : Write a JAVA Program to print the Fibonacci Series .

Posted on:
tags: ,


   Write a JAVA Program to print the Fibonacci Series .
import java.io.*;
public class fibo {
     public static void main(String argc[]) throws IOException
    {
        fibonacci ob1 = new fibonacci();
        ob1.input();
        ob1.call();
     
    }


}
class fibonacci
{
     public int n,a,b,c,i;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    void input() throws IOException
    {
        System.out.println("ENTER THE NUMBER :: ");
        n = Integer.parseInt(br.readLine());

    }
    void call()
    {
        a = 0;
        b = 1;
        System.out.println("THE  FIBONACCI SERIES IS ::");
        System.out.print(a+" , ");
       System.out.print(b);
        for(i = 2; i <= n; i++)
        {
            c = a + b;
            a = b;
             b = c;
            
             System.out.print(" , "+c);

        }

    }

}
OUTPUT:
ENTER THE NUMBER ::
7
THE  FIBONACCI SERIES IS ::
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13

No comments:

Post a Comment

< >