Print Fibonacci series - Java program

Q. Write a program in Java to print Fibonacci series up to given number.

Answer:

The Fibonacci series is the series of numbers in which each number is the sum of the two preceding numbers.

Fibonacci.java

import java.util.Scanner;
class Fibonacci
{
        public static void main(String[] args)
        {
                int f1=0, f2=1;
                int fib=0, count;
                Scanner scn = new Scanner(System.in);
                System.out.print("Enter the number of term: ");
                int number = scn.nextInt();
                System.out.println("First "+number+" number of Fabonacci series.");
                for (count = 0;count < number ;count++ )
                {
                        if (count <= 1)
                        {
                                fib = count;
                        }
                        else
                        {
                                fib = f1+f2;
                                f1 = f2;
                                f2 = fib;
                        }
                        System.out.print(fib+" ");
                }
                System.out.println("\n\n\n\n");
        }
}


Output:

fibonacci