백준 문제풀이

10870번 : 피보나치 수 5 (자바, Java)

뮤츠 2022. 10. 1. 22:34

팩토리얼 문제와 마찬가지로, 매우 쉽고 재귀를 꼭 쓸 필요도 없지만, 연습삼아 재귀함수로 푸는 문제.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		
		System.out.println(fibo(n));
		
		

	}
	
	public static int fibo(int num) {
				
		if (num == 0) {
			
			return 0;
			
		} else if (num == 1) {
			
			return 1;			
		} else {		
			
			return fibo(num-1) + fibo(num-2);
			
		}		
		
	}

}