In this example, you will learn how to Display Fibonacci series upto a given number (instead of terms) 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:"); int n = scanner.nextInt(); int t1 = 0, t2 = 1; System.out.print("Upto " + n + ": "); while (t1 <= n) { System.out.print(t1 + " + "); int sum = t1 + t2; t1 = t2; t2 = sum; } } |
Output: