Java Program to Find the Fibonacci Series

Introduction

The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. The series begins as follows: 0, 1, 1, 2, 3, 5, 8, 13, and so on. The Fibonacci series has numerous applications in computer science, mathematics, and finance. This guide will walk you through writing a Java program that generates the Fibonacci series up to a specified number of terms.

Problem Statement

Create a Java program that:

  • Prompts the user to enter the number of terms in the Fibonacci series.
  • Generates and displays the Fibonacci series up to the specified number of terms.

Example:

  • Input: 7
  • Output: "0, 1, 1, 2, 3, 5, 8"

Solution Steps

  1. Read the Number of Terms: Use the Scanner class to take the number of terms as input from the user.
  2. Generate the Fibonacci Series: Use a loop to generate the Fibonacci series.
  3. Display the Fibonacci Series: Print the series up to the specified number of terms.

Java Program

// Java Program to Find the Fibonacci Series
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class FibonacciSeries {
    public static void main(String[] args) {
        // Step 1: Read the number of terms from the user
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter the number of terms: ");
            int terms = scanner.nextInt();
            
            // Step 2: Generate the Fibonacci series
            System.out.print("Fibonacci Series: ");
            generateFibonacciSeries(terms);
        }
    }
    
    // Method to generate Fibonacci series
    public static void generateFibonacciSeries(int terms) {
        int first = 0, second = 1;
        
        for (int i = 1; i <= terms; i++) {
            System.out.print(first + " ");
            
            // Calculate the next number in the series
            int next = first + second;
            first = second;
            second = next;
        }
    }
}

Explanation

Step 1: Read the Number of Terms

  • The Scanner class is used to read an integer input from the user, which represents the number of terms in the Fibonacci series. The nextInt() method captures this input.

Step 2: Generate the Fibonacci Series

  • The generateFibonacciSeries() method generates the Fibonacci series up to the specified number of terms.
  • The method starts with the first two numbers in the series, first = 0 and second = 1.
  • A loop runs terms times, printing the current number and calculating the next number in the series as the sum of the previous two numbers.

Step 3: Display the Fibonacci Series

  • The Fibonacci series is printed in sequence using System.out.print() inside the loop.

Output Example

Example:

Enter the number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8 

Conclusion

This Java program demonstrates how to generate and display the Fibonacci series up to a specified number of terms. It covers essential concepts such as loops, basic arithmetic operations, and user input handling, making it a valuable exercise for beginners learning Java programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top