Friday, December 9, 2011

Write a java Program to print Fibonacci up to n (10) terms.


Aim : Write a java Program to print Fibonacci up to  n (10) terms.

//importing io classes
import java.io.*;


class  Fibonacci_rec
{
public int fibo(int n)
{
if (n == 1)
return 1;
else if (n == 2)
return 1;
else
return fibo(n-1) + fibo(n-2);
}
/*
This method will do exactly that, it will invoke the two previous versions of itself,
and those invoked will invoke their previous selves and so on until one of them reaches
f(2) or f(1). It's easy to use this method in a loop to print out many Fibonacci numbers:
*/


public static void main(String[] args)
{

Fibonacci_rec f=new Fibonacci_rec();
for (int i=1; i<10; i++)
System.out.println(f.fibo(i));


}
}

No comments:

Post a Comment