Fibonacci series program in java

Fibonacci series program in java

import java.util.Scanner;

public class Fibonacci
{
public static void main(String args[])
{
int n, i, first = 0, second = 1, next;

System.out.println("Enter the number of terms");
Scanner enter = new Scanner(System.in);
n = enter.nextInt();

System.out.println("Fibonacci series are");
for (i = 0 ; i < n ; i++)
{
if (i <= 1)
next = i;
else
{
next = first + second;
first = second;
second = next;
}
System.out.println(next);
}

}
}

Output….

Enter the number of terms
5
Fibonacci series are
0
1
1
2
3

Previous articleFibonacci series program in c| write a program to print fibonacci series in c.
Next articlePrime Number Program in Java | write a program to print Prime Number Program in Java.

LEAVE A REPLY

Please enter your comment!
Please enter your name here