Home / Programs / Program to display the fibonacci series based on the user input
🚀 Programming Example

Program to display the fibonacci series based on the user input

👁 5,967 Views
💻 Practical Program
📘 Step Learning
Write a program to find out the Fibonacci number using user input.: This program display the sequence based on the number entered by user. For example – if user enters 10 then this program disp

💻 Program Code

import java.util.Scanner;
public class JavaExample {

    public static void main(String[] args) {

        int count, num1 = 0, num2 = 1;
        System.out.println("How may numbers you want in the sequence:");
        Scanner scanner = new Scanner(System.in);
        count = scanner.nextInt();
        scanner.close();
        System.out.print("Fibonacci Series of "+count+" numbers:");

        int i=1;
        while(i<=count)
        {
            System.out.print(num1+" ");
            int sumOfPrevTwo = num1 + num2;
            num1 = num2;
            num2 = sumOfPrevTwo;
            i++;
        }
    }
}
                        

🖥 Program Output

How may numbers you want in the sequence:
10
Fibonacci Series of 10 numbers:0 1 1 2 3 5 8 13 21 34 
                            

📘 Explanation

None
📚 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.