Fibonacci series in java

This tutorial will help you to understand the Fibonacci number program in java

Fibonacci series in java

This tutorial will help you to understand the Fibonacci number program in java

Fibonacci series in java

Fibonacci series in java

In this section you will learn about fibonacci number in java.  Fibonacci number or Fibonacci sequence are the number in the following sequence  0, 1, 1, 2, 3, 5, 8, 13, 21....... The first two value in the sequence are 1, 1. Every subsequent value is the sum of the two value proceeding it. Here is the code for Fibonacci program:

import java.util.Scanner;
public class FibonacciExample {

 public static void main(String args[])
{
 int a=0,b=1,c;
 System.out.println("");
 System.out.println("Enter the number");
 Scanner sin=new Scanner(System.in);
 int n=sin.nextInt();
 System.out.println("Fibonacci numbers are :");
 System.out.print(a);
 System.out.print(" "+b);
 for(int i=0;i<n;i++)
  {
    c= a + b;
    System.out.print(" "+c);
    a=b;
    b=c;
   }
  }
 }

Description : In the above code getting the input from the user, based on the input provided by user loop will be executed and the first two values are 1, 1 and remaining is added with their preceding term inside a loop and then print by println() method.

Output : When you will compile and execute the program output will look like as follows:

Download SourceCode