Tuesday, 2 July 2013

Fibonacci Series in Java

/*
To Print n Fibonacci Numbers (Fibonacci Series)

1 1 2 3 5 8 13 21 ......n .

Fibonacci Series is also believed to start from 0 in many colleges depending upon the acceptance .
To modify this prog to print 0 1 1 2 3 5 8 21 ......n.
Change the value of x=0 .
*/


import java.io.*;

class Fibo
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

int x,y,z;

System.out.println("ENTER THE NO UPTO WHICH FIBONACCI SERIES REQUIRED");
int n = Integer.parseInt(input.readLine());

System.out.println("The First "+ n +" Fibonacci Numbers are as follows");

x=1;
y=1;

System.out.print(x+" "+y+" ");


for(int i=1;i<=(n-2);i++)
{
z=x+y;
System.out.print(z+" ");
x=y;
y=z;
}

}
}

No comments:

Post a Comment