Examples
- Hello World Program in Java (Print Hello World in Java)
- Java Program to Add Two Numbers (Java Sum / Addition)
- Find Greatest of Three Numbers in Java (Largest Number Program)
- Prime Number Program in Java (Code to Check Prime or Not)
- Java Program for Fibonacci Series (Using for, while, recursion, scanner)
- Factorial Program in Java (Find Factorial of a Number in Java)
- How to Find Sum of Digits of a Number in Java?
- How to Reverse a Number in Java? Program & Examples
- How to Swap Two Numbers in Java? Programs With/Without Third Variable
- Even Odd Program in Java (Program to Check Number is Even or Odd)
- Vowel and Consonant Program in Java
- Java Program for Quadratic Equation (Find Roots With 3 Ways)
- Find Frequency of Characters in a String in Java (4 Ways)
- Remove Space from String in Java (Remove Whitespace)
- String Null Check in Java (String is Empty or Null) - 5 Ways
- How to Print String in Java? 6 Methods
- How to Get ASCII Value of Char in Java? Find ASCII Value
Java Program for Fibonacci Series (Using for, while, recursion, scanner)
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers. It starts with 0 and 1, and then each subsequent number is the sum of the previous two. The series goes like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.
In this tutorial, we will learn about the Java program for Fibonacci series using for loop, while loop, recursion, and scanner. Each method has its own unique approach, making it a great opportunity to understand different programming techniques and learn more about Java language features.
Let’s get started and practice the Fibonacci series program in Java!
Concepts to Learn for This Program:
Java Program for Fibonacci Series Using while loop
Code
class Fibonacci {
public static void main(String[] args) {
int n = 200, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series Upto " + n + ": ");
while (firstTerm <= n) {
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Output
Fibonacci Series Upto 200:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
Explanation
The program generates and prints the Fibonacci series up to the value of n, separated by commas. For n = 200, the output will be the Fibonacci series from 0 to the highest Fibonacci number less than or equal to 200.
Fibonacci Series in Java Using for loop
Here's a Java program to print the Fibonacci series using a for loop:
Code
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms in the Fibonacci series: ");
int numTerms = scanner.nextInt();
scanner.close();
int firstTerm = 0;
int secondTerm = 1;
System.out.print("Fibonacci Series up to " + numTerms + " terms: ");
for (int i = 1; i <= numTerms; i++) {
System.out.print(firstTerm + " ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Output
Enter the number of terms in the Fibonacci series: 8
Fibonacci Series up to 8 terms: 0 1 1 2 3 5 8 13
Explanation
In this program, we use a Scanner to read the user's input from the console. The program prompts the user to enter the number of terms they want in the Fibonacci series. The entered value is stored in the numTerms variable.
The program then uses a for loop to generate the Fibonacci series up to the specified number of terms. The first two terms are initialized to 0 and 1. The loop calculates the next term by adding the previous two terms and updates the variables accordingly.
Fibonacci Series Using Recursion in Java
Here's a Fibonacci series program in Java using recursion:
Code
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms in the Fibonacci series: ");
int numTerms = scanner.nextInt();
scanner.close();
System.out.print("Fibonacci Series up to " + numTerms + " terms: ");
for (int i = 0; i < numTerms; i++) {
System.out.print(fibonacci(i) + " ");
}
}
public static int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
Output
Enter the number of terms in the Fibonacci series: 10
Fibonacci Series up to 10 terms: 0 1 1 2 3 5 8 13 21 34
Explanation
In this program, we use a Scanner to read the user's input from the console. The program prompts the user to enter the number of terms they want in the Fibonacci series. The entered value is stored in the numTerms variable.
The program then uses a for loop to generate the Fibonacci series up to the specified number of terms. For each term index i, it calls the fibonacci() method to calculate the value of the Fibonacci number at that index.
The fibonacci() method is a recursive function that calculates the Fibonacci number for a given index n. If n is 0 or 1, it returns n. Otherwise, it recursively calls itself with the previous two indices (n-1 and n-2) and sums their Fibonacci numbers to calculate the Fibonacci number for index n.
Learn Next: