-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibRec.java
More file actions
48 lines (34 loc) · 703 Bytes
/
FibRec.java
File metadata and controls
48 lines (34 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/// find the Fibnaccinusing the Recursion
import java.util.*;
public class FibRec
{
public static void main(String args[])
{
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the natural Number: ");
n=sc.nextInt();
System.out.print("Fibonacci Series: ");
for(int i=0;i<n;i++)
System.out.print(" "+fib(i));
int a=0,b=1,c=0;
System.out.println();
System.out.print("Fibonacci Series: "+a+" "+b+" ");
for(int i=0;i<n-2;i++)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}
}
static int fib(int n)
{
if(n==0)
return 0;
if(n==1||n==2)
return 1;
else
return (fib(n-1)+fib(n-2));
}
}