본문 바로가기

CodingTest Practice

[인프런] 이진수 출력

이진수 출력

- 10진수 N이 입력되면 2진수로 변환하여 출력하는 프로그램을 작성

- 재귀함수를 이용해서 출력

 

■ 문제 풀이

1) 입력 설명
- 첫 번째 줄에 10진수 N (1<=N<=1,000)이 주어진다.

2) 출력 설명
- 첫 번째 줄에 이진수를 출력

3) 테스트
- input : 11
- output : 1011

4) 재귀함수 호출 

 

import java.util.Scanner;

public class Main {

    private void recursive(int input) {
        if (input == 0) {
            return;
        } else {
            recursive(input / 2);
            System.out.print(input % 2);
        }
    }

    public static void main(String[] args) {
        Main main = new Main();
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();

        main.recursive(input);
    }
}

 

'CodingTest Practice' 카테고리의 다른 글

[인프런] 아나그램  (0) 2022.04.27
[인프런] 학급 회장  (0) 2022.04.26
[인프런] 재귀함수  (0) 2022.04.22
[인프런] 뒤집은 소수  (0) 2022.04.21
[인프런] 소수 구하기(에라토스테네스의 체)  (0) 2022.04.15