냠냠냠

java) 십진수 정수를 받아 이진수로 표현하기(재귀적 함수 이용) 본문

언어/JAVA

java) 십진수 정수를 받아 이진수로 표현하기(재귀적 함수 이용)

[Nada] 2019. 12. 2. 21:12

문제) 십진수 정수를 받아 이진수로 출력하기. (메소드를 재귀의 형태로 정의할 것)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MethodJustice {
 
    public static void main(String[] args) {
        trans(11);
    }
    
    public static int trans(int n) {
        if(n > 0) {
            int binary = n % 2;    // 1, 1, 0, 1
            n /= 2;    // 5, 2, 1, 0
            trans(n);    // trans(5), trans(2), trans(1), trans(0)
            System.out.print(binary);
        }
        return 0;
            
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

Comments