package com.algo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* Program for Fibonacci Series using Java
* Author: Guru.
* Date: 10-01-2011
*/
public class FibonacciSeries {
/**
* @param args
*/
public static void main(String[] args) {
int n=0;
System.out.println("Please Enter value for N:");
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
try {
//Read the febonacci value
n=Integer.parseInt(br.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//print the Fibonacci series numbers
System.out.println("The Febonicci Series is:");
for(int i=0;i<=n;i++)
{
System.out.print(febonacci(i)+" ");
}
}
/*
* The method for computing the Febonacci Series.
* input: int
*/
private static int febonacci(int n) {
if(n<=0) return 0;
else if(n==1) return 1;
else return(febonacci(n-1)+febonacci(n-2));
}
}
Output of Febonacci Series Java Program:
Please Enter value for N:
8
The Febonicci Series is:
0 1 1 2 3 5 8 13 21