Code:
import java.util.Scanner;
class fibonacci
{
public static void main(String[] args)
{
int n,a=0,b=1;
Scanner in=new Scanner(System.in);
System.out.print("Enter limit: ");
n=in.nextInt();
if (n>1)
{
System.out.println("0"+"\n"+"1");
}
for (int i=3;i<=n;i++)
{
int c=a+b;
if (c<=n)
{
System.out.println(c);
a=b;
b=c;
}
}
}
}
class fibonacci
{
public static void main(String[] args)
{
int n,a=0,b=1;
Scanner in=new Scanner(System.in);
System.out.print("Enter limit: ");
n=in.nextInt();
if (n>1)
{
System.out.println("0"+"\n"+"1");
}
for (int i=3;i<=n;i++)
{
int c=a+b;
if (c<=n)
{
System.out.println(c);
a=b;
b=c;
}
}
}
}
No comments:
Post a Comment