Fibonacci Series in Java

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.

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

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment