My Bookmarks

  • No bookmark found

Most Active Members

Write a program to find Fibonacci series for a given input in Java1 min read

AIM: – Write a program to find Fibonacci series for a given input.

Here we will Write a program to find Fibonacci series for a given input in Java to help this java world members.

Hope so you would like it, we are trying to make program easier by writing comment with it.

Source Code: –

/* In Fibonacci series the first and second terms are fixed i.e. 0 and 1 respectively.
* and the subsequent terms are the sum of last two previous terms.
*/
import java.util.Scanner;

public class Fibonacci {

    public static void main(String[] args) {

        int num, first = -1, second = 1, third = 0;
        Scanner scanner = new Scanner(System.in);

        //Reading the number of terms to be displayed in the series
        System.out.println("Enter the value of n:- ");
        num = scanner.nextInt();

        /*Finding the next term in the series
        * and printing the series inside the for-loop
       .*/
        System.out.println("The Fibonacci series is shown below:- ");
        for(int i=0; i<num; i++) {

            third = first + second;
            first = second;
            second = third;

            System.out.print(third+" ");
        }
    }
}

Output :

Bookmark(0)
Tags:

Add a Comment

Your email address will not be published. Required fields are marked *