Showing posts with label Write a program in Java to make Fibonacci series. Show all posts
Showing posts with label Write a program in Java to make Fibonacci series. Show all posts

Thursday, 28 August 2014

Fibonacci Series in Java

Structure of the Problem Requirements 

In this problem we will learn how to make a Fibonacci Series in Java Program. Fibonacci series is a mathematical problem in which the third term is the sum of previous two terms. Fibonacci series is like  1 1 2 3 5 8 and it clear that the previous two terms make third term . Here is the source code of this example which help you in better understanding.

Source Code


import java.util.Scanner;

public class Fibonacci{
public static void main(String[] args) {
    Scanner input = new Scanner (System.in);
    int num;
    long previous =1, next =0,sum;
    System.out.println(" Enter Your Number to Find its Fibonacci ");
    num = input.nextInt();
    while (next<num/2)
    {
        System.out.print(previous+ " ");
        sum = previous+next;
        next = previous;
        previous = sum;
    }
   }
  }

Output of the Program

Fibonacci Series
Fibonacci Series output