//Fibonacci Series using Recursion
class fibonacci
{
static int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
public static void main (String args[])
{
int n = 9;
System.out.println(fib(n));
}
}
//Iterative Fibonacci series program in Java
class Fibonacci{
public static void main(String args[])
{
//Considering the first two numbers as 0 and 1
int num1=0,num2=1,num3,i,count=10;
//Count=10 means that only the first 10 fibonacci numbers will be displayed
System.out.print(num1+" "+num2);
//printing 0 and 1
for(i=2;i<count;++i)
//looping is initiated from 2 as 0 and 1 are already printed
{
num3=num1+num2;
System.out.print(" "+num3);
num1=num2;
num2=num3;
}
}}
class Main {
public static void main(String[] args) {
int n = 10, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
for (int i = 1; i <= n; ++i) {
System.out.print(firstTerm + ", ");
// compute the next term
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}