In this example, you will learn how to Display Fibonacci series using for while in Java
Fibonacci java code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object System.out.print("Enter a number for count:"); int n = scanner.nextInt(); int i = 1, t1 = 0, t2 = 1; System.out.print("First " + n + " terms: "); while (i <= n) { System.out.print(t1 + " + "); int sum = t1 + t2; t1 = t2; t2 = sum; i++; } } |
Output: