Write a Java Program which will accept limit from user and print the Fibonacci series | EasyCoding45

Write a Java Program which will accept limit from user and print the Fibonacci series.







import java.util.Scanner;

public class FibonacciSeries {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of terms for Fibonacci series: ");
        int numTerms = scanner.nextInt();
        scanner.close();

        int firstTerm = 0;
        int secondTerm = 1;

        System.out.println("Fibonacci Series:");
        for (int i = 1; i <= numTerms; ++i) {
            System.out.print(firstTerm + " ");

            // Compute the next term in the series
            int nextTerm = firstTerm + secondTerm;
            firstTerm = secondTerm;
            secondTerm = nextTerm;
        }
    }
}

Output : 

Write a Java Program which will accept limit from user and print the Fibonacci series


Comments