 ## Super friendly code & developer communities

A place for coders and developers to share, learn and grow together.

# Java Programming | Fibonacci Series

This is a tutorial for Fibonacci series in Java. The program is given below that get the number from user and print the Fibonacci Series. The program is not extendable. Go enjoy the program. Lets begin…

Program for Fibonacci Series in Java.

``````
//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
public class FibonacciSeries {
//void main
public static void main (String[] args)
{
//declare int variables
int x=0,y=1,z,n;
//print message
System.out.println("How many numbers do you want?");
//Take input
Scanner input = new Scanner(System.in);
n = input.nextInt();
//print the first two numbers manually.
System.out.println("Fibonacci Series:-");
System.out.println(x);
System.out.println(y);
//go on...
//print the fibonacci series.
for(int i=0;i<n-2;i++)
{
z=x+y;
x=y;
y=z;
System.out.println(z);
}
}
}
``````

Output

How many numbers do you want?
7
Fibonacci Series:-
0
1
1
2
3
5
8

How does it work
1. You enter the number.
2. The for loop run and calculates the number of Fibonacci series and prints it.

Explanation.
1. Import the Scanner.
2. Declare the class as public
3. Add the void main function
4. Add system.out.println() function with the message to enter choice.
5. Declare input as Scanner.
6. Take the inputs and save it in variables.
7. Manually print the first two numbers.