How to generate Fibonacci series in java

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int first=0,second=1,third;
        System.out.println("Enter how many term(s): ");        Scanner scanner = new Scanner(System.in);        int input=scanner.nextInt();        scanner.close();
            for (int i = 1; i <=input; i++) {
                System.out.println(first);                 third = first + second;
                first=second;                second=third;            }
    }
}
  • The above code is represented Fibonacci series.
Output :

Enter how many term(s):  5
0
1
1
2

3

Comments

Popular posts from this blog

Basic calculator using C programming

Factorial of consecutively 5 numbers(input)

Square Root of a number without using math.h function