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 Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 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);
    }
}

                        
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.