Write a program in java print the fibonacci series within given range.
Example: 0, 1, 1, 2, 3, 5, 8, 13..........n th terms.
Program:
import java.util.*;
class Fibonacci
{
public void func(int n)
{
int i, a=0, b=1, s;
System.out.print(a + "," + b + ",");
for(i=3;i<=n;i++)
{
s=a+b;
System.out.print(s+",");
a=b;
b=s;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Limit: ");
int limit=sc.nextInt();
Fibonacci ob = new Fibonacci();
ob.func(limit);
}
}
Output:
Enter The Limit:
10
0,1,1,2,3,5,8,13,21,34,
Example: 0, 1, 1, 2, 3, 5, 8, 13..........n th terms.
Program:
import java.util.*;
class Fibonacci
{
public void func(int n)
{
int i, a=0, b=1, s;
System.out.print(a + "," + b + ",");
for(i=3;i<=n;i++)
{
s=a+b;
System.out.print(s+",");
a=b;
b=s;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Limit: ");
int limit=sc.nextInt();
Fibonacci ob = new Fibonacci();
ob.func(limit);
}
}
Output:
Enter The Limit:
10
0,1,1,2,3,5,8,13,21,34,