This is a Java Program to Generate Fibonacci Numbers. The number is said to be in a Fibonacci series if each subsequent number is the sum of the previous two numbers.
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.
import java.util.Scanner;
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();
System.out.print(“Fibonacci Series:”);
for(int i = 1; i <= n; i++)
{
a = b;
b = c;
c = a + b;
System.out.print(a+" ");
}
}
}
Output:
$ javac Fibonacci.java
$ java Fibonacci
Enter value of n:5
Fibonacci Series:0 1 1 2 3