본문 바로가기

Projects/백준 문제

Baekjoon 10870 Java Recursive Fibonacci number

반응형

	// Recursive method for getting the fibonacci number for N
	public static int Fibonacci(int N) {
		if(N == 0) return 0;
		if(N == 1) return 1;
		
		return Fibonacci(N-1) + Fibonacci(N-2);
	}
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int N = sc.nextInt();
		
		System.out.println(Fibonacci(N));
		

		
		

	}

	// Recursive method for getting the fibonacci number for N
	public static int Fibonacci(int N) {
		if(N == 0) return 0;
		if(N == 1) return 1;
		
		return Fibonacci(N-1) + Fibonacci(N-2);
	}
	
	
}
반응형