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 by 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++;
        }
    }
}

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

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.