The following Java code snippet results in Fibonacci series up to your choice.
- import java.util.Scanner;
-
- public class FibonacciSeries
- {
- public static void main(String[] args)
- {
- int limit;
- Scanner in = new Scanner(System. in );
- System.out.print("Enter the series limit: ");
- limit = in .nextInt();
- long[] series = new long[limit];
-
- series[0] = 0;
- series[1] = 1;
-
- for (int i = 2; i < limit; i++)
- {
- series[i] = series[i - 1] + series[i - 2];
- }
-
- System.out.println("Fibonacci Series upto limit " + limit + " is:");
- for (int i = 0; i < limit; i++)
- {
- System.out.print(series[i] + " ");
- }
- System.out.println();
- }
- }
Thank you, keep learning and sharing.