import java.io.*;
public class FibonacciSeries
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter number upto you want Fibonacci Series to be :: ");
int n = Integer.parseInt(br.readLine());
int a = 0, b = 1, c;
System.out.print("\nFibonacci Series upto " + n + " is:: \n" + a + "\t" + b);
c = a + b;
while (c <= n)
{
System.out.print("\t" + c);
a = b;
b = c;
c = a + b;
}
}
}
OUTPUT::
Enter number upto you want Fibonacci Series to be :: 12
Fibonacci Series upto 12 is::
0 1 1 2 3 5 8
public class FibonacciSeries
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter number upto you want Fibonacci Series to be :: ");
int n = Integer.parseInt(br.readLine());
int a = 0, b = 1, c;
System.out.print("\nFibonacci Series upto " + n + " is:: \n" + a + "\t" + b);
c = a + b;
while (c <= n)
{
System.out.print("\t" + c);
a = b;
b = c;
c = a + b;
}
}
}
OUTPUT::
Enter number upto you want Fibonacci Series to be :: 12
Fibonacci Series upto 12 is::
0 1 1 2 3 5 8
Using Recursive Method::
import java.io.*;
public class FibonacciSeriesRecursive
{
static int fibo(int n)
{
if (n <= 2)
return 1;
else
return fibo(n - 1) + fibo(n - 2);
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter number upto u want Fibonacci Series to be:: ");
int n = Integer.parseInt(br.readLine());
System.out.print("\nFibonacci Series upto " + n + " terms is::" + "\n"
+ "0"); // First number is 0
for (int i = 1; i <= n; i++)
{
int f = fibo(i);
System.out.print(" " + f);
}
}
}
OUTPUT::
Enter number upto u want Fibonacci Series to be:: 12
Fibonacci Series upto 12 terms is::
0 1 1 2 3 5 8 13 21 34 55 89 144