Fibonacci Series Java Program Using for Loop

Introduction

The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting from 0 and 1. This guide will show you how to generate the Fibonacci series in Java using a for loop.

Problem Statement

Create a Java program that:

  • Generates a specified number of Fibonacci numbers using a for loop.

Example 1:

  • Input: 10 (first 10 Fibonacci numbers)
  • Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Example 2:

  • Input: 5 (first 5 Fibonacci numbers)
  • Output: 0, 1, 1, 2, 3

Solution Steps

  1. Initialize Variables: Set up variables to hold the first two Fibonacci numbers (0 and 1).
  2. Use a for Loop to Generate the Series: Loop through the required number of terms, calculating each new Fibonacci number by summing the previous two.
  3. Print the Series: Output each Fibonacci number as it is calculated.

Java Program

import java.util.Scanner;

/**
 * Java Program to Generate Fibonacci Series using for Loop
 * Author: https://www.javaguides.net/
 */
public class FibonacciForLoop {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Get the number of terms from the user
        System.out.print("Enter the number of Fibonacci terms: ");
        int n = scanner.nextInt();

        // Step 2: Initialize the first two Fibonacci numbers
        int first = 0, second = 1;

        // Step 3: Generate and print the Fibonacci series
        System.out.println("First " + n + " Fibonacci numbers:");
        for (int i = 0; i < n; i++) {
            if (i == 0) {
                System.out.print(first);
            } else if (i == 1) {
                System.out.print(", " + second);
            } else {
                int next = first + second;
                System.out.print(", " + next);
                first = second;
                second = next;
            }
        }
        System.out.println();
    }
}

Explanation

Step 1: Get the Number of Terms from the User

  • The program prompts the user to enter how many terms of the Fibonacci series they would like to generate.

Step 2: Initialize the First Two Fibonacci Numbers

  • Two variables, first and second, are initialized to 0 and 1, respectively. These represent the first two Fibonacci numbers.

Step 3: Generate and Print the Fibonacci Series

  • A for loop is used to iterate from 0 to n-1:
    • First and Second Terms: The first and second terms are printed directly.
    • Subsequent Terms: For each subsequent term, the next Fibonacci number is calculated by summing the first and second numbers. The first is then updated to the value of second, and second is updated to the newly calculated Fibonacci number.
  • The numbers are printed in a comma-separated format.

Output Examples

Example 1:

Enter the number of Fibonacci terms: 10
First 10 Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Example 2:

Enter the number of Fibonacci terms: 5
First 5 Fibonacci numbers:
0, 1, 1, 2, 3

Conclusion

This Java program efficiently generates the Fibonacci series using a for loop. This method is straightforward and works well for generating a specified number of terms in the Fibonacci sequence. The use of a loop ensures that the program is both simple and efficient, making it suitable for a wide range of applications.

Comments