April 08, 2015

Fibonacci series implementation in java

How to find fibonacci series of a given number source code

package com.blogspot.javabelazy.programs;
import java.util.Scanner;
/*
 * Fibinocci number series implementaion in java
 * Series : 0, 1, 1, 2, 3, 5, 8, 13
 */


/**
 * @author vipin
 * Fibinocci Series Java full source code download
 */
public class FibinocciSeries {

/**
* @param args
*/
public static void main(String[] bestJavaBlog) {

System.out.println(" Enter the nth number to find the fibinocci value ");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();  // user input
//int input = 99;
int previousNumber = 0;
int number = 1;
int nextNumber = 0;
System.out.println("\n Fibinocci series upto "+input +"th number ");
for(int i=0;i<input;i++){
if(i==0){
//initialize
System.out.print(" "+previousNumber);

}else if(i==1){
System.out.print(", "+number);
}
else{
nextNumber = previousNumber + number;
System.out.print(", "+nextNumber);
previousNumber = number;
number = nextNumber;
}
}
System.out.println("\n Created by http://www.javabelazy.blogspot.in ");
}
}

Output

Fibonacci series source code
Fibonacci Series implementation in java



Fibonacci Series Algorithm


1. Start
2. Decalare variables incr, first, second, value
3. Initialize the variables first  as 0, value  as 0 and second as 1
4. Enter the nth term of the Fibonacci to be outputted
5. Print first two terms
6. Then loop the below code upto the nth term
    value = first + second
    first = second
    second = value
    increment variable incr each time by 1
7. Print the value in the variable value
8. Stop


About Fibonacci Sequence

The Fibonacci Series is a series of number where the next number is found by adding up last two number in the series.

Series : 0,1,1,2,3,5,8,13,21,34 and so on

Fibonacci Number, their ration is very close to Golden ration "phi" which is equal to 1.618.

Real world example : Fibonacci considers the growth of shell of snail, Petals of flower




Author : +ITSECTION CIVILSUPPLIES 

http://javabelazy.blogspot.in/

Facebook comments