Home / Programs / Write a java program to print fibonacci series... The number of terms required, is to be passed as parameter.
Programming Example

Write a java program to print fibonacci series... The number of terms required, is to be passed as parameter.

👁 14 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:


Expected Output:

Fibonacci Series:
0 1 1 2 3 5 8 13 21 34

Program Code

public class FibonacciSeries {

    // Method to print Fibonacci series
    public static void printFibonacci(int n) {
        int first = 0, second = 1;

        System.out.println("Fibonacci Series:");

        for (int i = 1; i <= n; i++) {
            System.out.print(first + " ");

            int next = first + second;
            first = second;
            second = next;
        }
    }

    public static void main(String[] args) {
        int terms = 10;   // Number of terms passed as parameter
        printFibonacci(terms);
    }
}

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.