Fibonacci Sequence:
Basic rule:: next term = Sum of previous two terms
Code::
import java .util.*;
public class Fibonacci
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter Number of Terms:");
int a=in.nextInt();
for(int i=0;i<a;i++)
{
System.out.print(fibonacci(i)+" ");
}
}
public static int fibonacci(int n)
{
if(n<2)
{
return n;
}
else
{
return (fibonacci(n-2)+fibonacci(n-1));
}
}
}
Output::
Basic rule:: next term = Sum of previous two terms
Code::
import java .util.*;
public class Fibonacci
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter Number of Terms:");
int a=in.nextInt();
for(int i=0;i<a;i++)
{
System.out.print(fibonacci(i)+" ");
}
}
public static int fibonacci(int n)
{
if(n<2)
{
return n;
}
else
{
return (fibonacci(n-2)+fibonacci(n-1));
}
}
}
Output::