Skip to content

Instantly share code, notes, and snippets.

@BetelGeuseee
Created August 23, 2020 11:08
Show Gist options
  • Save BetelGeuseee/8f25761c526deadfe75c9c9a049cd3f7 to your computer and use it in GitHub Desktop.
Save BetelGeuseee/8f25761c526deadfe75c9c9a049cd3f7 to your computer and use it in GitHub Desktop.
Fibonacci series using recursion in JAVA
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