Created
August 23, 2020 11:08
-
-
Save BetelGeuseee/8f25761c526deadfe75c9c9a049cd3f7 to your computer and use it in GitHub Desktop.
Fibonacci series using recursion in JAVA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class FibonacciRecursion { | |
static int first=0,second=1,next=0; | |
static void generateFibo(int range){ | |
if(range>0){ | |
next=first+second; | |
first=second; | |
second=next; | |
System.out.print(next+" "); | |
generateFibo(range-1); | |
} | |
} | |
public static void main(String[] args){ | |
Scanner scan = new Scanner(System.in); | |
int ran=scan.nextInt(); | |
if(ran==1){ | |
System.out.print(0); | |
} | |
else if(ran==2) { | |
System.out.print(0 + " " + 1 + " "); | |
}else { | |
System.out.print(0 + " " + 1 + " "); | |
generateFibo(ran - 2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment