# Fibonacci series in Java using recursion

## Fibonacci series in Java using recursion

### Fibonacci series in practical:

Fibonacci series using recursion 1 :
import java.util.Scanner;
public class Main
{
public static int Fibonacci(int n)
{
if (n <= 1)
return n;
return Fibonacci(n – 1) + Fibonacci(n – 2);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n, sum = 0;
System.out.println(“Enter the nth value: “);
n = sc.nextInt();
System.out.println(“Fibonacci series: “);
while(Fibonacci(sum) <= n)
{
System.out.print(Fibonacci(sum) + ” “);
sum++;
}
}
}
Fibonacci series using recursion 2 :
class FibonacciExample2{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(” “+n3);
printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(n1+” “+n2);//printing 0 and 1
printFibonacci(count-2);//n-2 because 2 numbers are already printed
}
}

### Fibonacci series in Theory :

we’ll talk about to write a  code to find a
Fibonacci series so we’ll bring this
then one basically we have before one
will be having a 0 so 0 plus 1 is 1 then
this 1 plus 1 is 2 then 1 plus 2 is 3 2
plus 3 is 5 3 + 5 is 8 and 5 plus 8 is
13 and so so on so when to plane this
series still our number n ok so anyway n
may be any valuable it’s a 50 or 70 or 2
hundreds maybe it may be any number so
in order to implement Fibonacci series
what we’ll do is we’ll use a main
function here which is obviously void
main now so for this we need some
variables so first we’ll say a variable
is K then a variable a and B just to
make it simple so we have these three
variables by default the value of K will
be 0 ok we’ll run a while loop till what
point I want to go with this series so
we’ll say till the point let’s say 50 so
maximum value I want is 50 ok or equal
to 50 m so I don’t know if 50 lies
between this series but let’s say if
this values is less than is equal to 50
then we have to print now what we to do
is we will say the initial value for
this this a is 1 and B is also 1 okay
and let’s print the value of a and B so
how to print the value of a and B will
say us out and we’ll print without
giving us new line will print the value
1 and 1 because this is what we want
right 1 1 and then I will find K so k is
equal to a plus B now when you say a
plus B so a value is 1 and B value is 1
which simply means this is 1 and 1 the K
value will be 2 and now let us print
that too so how to print 2 will say
let’s print the value of K with
space okay now if you plan this if I
high speed space here also knife Adam
this it will print one one and the next
K value which is K simple now next in
the next iteration what I do is I will
just change the value of a as one and
and the be ash tube so what I mean by
that is I can simply say a equal to B so
the value of a becomes P so we have to
shift those parameters and
the value of B will be key
okay and now if you shape this it will
bring the whole series so that’s it we
are just printing values and then we are
saying yeah we’re painting values right
so if I run this now so you can this is
your Fibonacci series so it is 1 1 and
all this values okay so since we are
checking we are checking first and then
adding the value so that’s why it is
fitting in this way okay so it is going
beyond 50 so you can also do here let’s
say if you can check with if condition
and again you can print it but that’s
the way we do it right so yeah so this
is how we are to print we can also take
this input which is value of 50 from
user so we can use a scanner plus here
to take the input so in that scenario we
have to use a scanner class will say
scanner is equal to new scanner okay I
am this we have to mention from where I
will be getting the input so with the
system input device I let’s import the
package so in that means you can just
simply type ctrl shift I which will
import the package and then we can
simply take will take a variable which
is n equal to SC dot next int which is
here okay and then we’ll ask user to
enter value so we’ll say enter any
number now instead of writing 50 here we
can type n okay if I save and if you run
this so it’s waiting for a number I will
say a 22 so that’s the answer so it’s
just go beyond 34 22 right so you can
also change it so
what you can do is you can simply say
before printing if a case less than
equal to n will say break but we have
multiple ways to solve this so this is
one of the way to solve that problem so
if I said you need to now just made a
mistake it should be greater so if I run
this and if I say 22 now so it will
print all the values till 21 okay if I
gave a different value so let’s say if I
gave one thousand and a big number
10,000 so it will print all the
Fibonacci series number you that’s the
last number because if you add this
number and this it will go beyond ten
thousand clear so this is how we have to
find the Fibonacci series
error: Content is protected !!