import java.io.*; class Fib { public static void main(String args[]) throws IOException { int n,f1,f2,f3; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter no.of elements in Fibonacci series: "); n = Integer.parseInt(br.readLine()); f1=0; f2=1; if(n>0) { System.out.println("Fibonacci Series:"); for(int i=0; i<n; i++) { System.out.println("\t"+f1); f3=f1+f2; f1=f2; f2=f3; } } else System.out.println("You Entered the Wrong Number..."); } }
OUTPUT:
Enter no.of elements in Fibonacci series: 6 Fibonacci Series: 0 1 1 2 3 5