What is Fibonacci Series in Java? Fibonacci Series Program in Java using For Loop Fibonacci Series Program in Java using While Loop Fibonacci Series Using Recursion in Java
Fibonacci Series Program in Java using For Loop
Output: Program Logic:
previousNumber is initialized to 0 and nextNumber is initialized to 1 The Fibonacci For Loop iterates through maxNumber
Display the previousNumber Calculates sum of previousNumber and nextNumber Updates new values of previousNumber and nextNumber
Fibonacci Series Program in Java using While Loop
You can also generate Java Fibonacci Series using a While loop in Java. Output: The only difference in the program logic is use of WHILE Loop to print Fibonacci Numbers
Fibonacci Series Based On The User Input
Program Logic: The logic is same as earlier. Instead of hardcoding the number of elements to show in Java Fibonacci Series, the user is asked to write number.
Fibonacci Series Using Recursion in Java
Below is a Fibonacci series program in Java using recursion:
Program Logic: A recursive function is one that has the capability to call itself. fibonacciRecursion():
The Java Fibonacci recursion function takes an input number. Checks for 0, 1, 2 and returns 0, 1, 1 accordingly because Fibonacci sequence in Java starts with 0, 1, 1. When input n is >=3, The function will call itself recursively. The call is done two times. Let’s see the Fibonacci Series in Java using recursion example for input of 4.
Now result is added 0+1+1+0+1=3