//file name--> Fibonacci.java
import java.io.*;
class Fibonacci{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int p=0,c=1,n=p+c,num;
System.out.println("Enter number of Series you want:");
num=Integer.parseInt(br.readLine());
for(int i=0;i < num-1;i++)
{
if(i==0)
System.out.print(p);
System.out.print("\t "+n);
n=c+p;
p=c;
c=n;
}
}
}
OUTPUT
javac Fibonacci.java
java Fibonacci
Enter number of Series you want:
10
0 1 1 2 3 5 8 13 21 34
import java.io.*;
class Fibonacci{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int p=0,c=1,n=p+c,num;
System.out.println("Enter number of Series you want:");
num=Integer.parseInt(br.readLine());
for(int i=0;i < num-1;i++)
{
if(i==0)
System.out.print(p);
System.out.print("\t "+n);
n=c+p;
p=c;
c=n;
}
}
}
OUTPUT
javac Fibonacci.java
java Fibonacci
Enter number of Series you want:
10
0 1 1 2 3 5 8 13 21 34