JAVA Program to generate a Fibonacci Series

import java.io.IOException;
import java.util.Scanner;

// Program to generate a Fibonacci Series

public class FibonacciSeries {
 
  public static void main(String args[])
      throws IOException {
   
    System.out.print("Enter the length of the series: ");
    Scanner in = new Scanner(System.in);
    int seriesLength = in.nextInt();
    in.close();
   
    long x=0, y=1;
    for (int i=0; i<seriesLength; i++) {
      System.out.print(x + " ");
      long t = y;
      y = x + y;
      x = t;
    }
  }
 
}
Advertisements