The following program prints fibonacci series.
import java.io.*;
class fibo
{
public static void main(String args[])throws Exception
{
int k=1,i=0,j=1;
System.out.println("Enter a value");
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
int n= Integer.parseInt(b.readLine());
System.out.println("fibonacci series is:");
System.out.print(k+" ");
for(int k1=0;k1<n-1;k1++)
{
k=i+j;
System.out.print(k+" ");
i=j;
j=k;
}
}
}
output:
Enter a value
5
fibonacci series is:
1 1 2 3 5
import java.io.*;
class fibo
{
public static void main(String args[])throws Exception
{
int k=1,i=0,j=1;
System.out.println("Enter a value");
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
int n= Integer.parseInt(b.readLine());
System.out.println("fibonacci series is:");
System.out.print(k+" ");
for(int k1=0;k1<n-1;k1++)
{
k=i+j;
System.out.print(k+" ");
i=j;
j=k;
}
}
}
output:
Enter a value
5
fibonacci series is:
1 1 2 3 5