Enter the number of terms you want as an input. Now we use for loop to generate the desired series.
Here is the source code of the Java Program to Generate Fibonacci Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Fibonacci
public static void main(String args)
int n, a = 0, b = 0, c = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter value of n:");
n = s.nextInt();
for(int i = 1; i <= n; i++)
a = b;
b = c;
c = a + b;
$ javac Fibonacci.java $ java Fibonacci Enter value of n:5 Fibonacci Series:0 1 1 2 3
Sanfoundry Global Education & Learning Series – 1000 Java Programs.