728x90
반응형
SMALL
//기수변환 메서드
//정수 x를 r진수로 변환하여 배열 d에 아랫자리부터 넣어두고 자릿수를 반환합니다.
static int cardConvR(int x, int r, char[] d){
int digits = 0; // 변환 후의 자릿수
String dChar = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
do{
d[digits++] = dChar.charAt(x % r); //r로 나눈 나머지를 저장
x /= r;
}while (x != 0);
return digits;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int no, cd, dno, retry;
char[] cno = new char[32];
System.out.println("10진수를 기수 변환합니다.");
do {
do {
System.out.print("변환하는 음이 아닌 정수 : ");
no = sc.nextInt();
}while (no < 0);
do {
System.out.print("어떤 진수로 변환할까요?(2~36) : ");
cd = sc.nextInt();
}while (cd < 2 || cd > 36);
dno = cardConvR(no, cd,cno);
System.out.print(cd + "진수로는 ");
for(int i = dno - 1; i >= 0; i--){
System.out.print(cno[i]);
}
System.out.println("입니다.");
System.out.println("한번 더 할 까요? 1. 예 / 2. 아니오");
retry = sc.nextInt();
}while (retry == 1);
}
실행결과
10진수를 기수 변환합니다.
변환하는 음이 아닌 정수 : 10
어떤 진수로 변환할까요?(2~36) : 2
2진수로는 1010입니다.
한번 더 할 까요? 1. 예 / 2. 아니오
728x90
반응형
LIST
'Algorithm' 카테고리의 다른 글
[Algorithm] 보초법 (0) | 2023.12.08 |
---|---|
[Algorithm] 선형 검색 (1) | 2023.12.08 |
[Algorithm] 배열 모든 요소 합계 (1) | 2023.12.05 |
[Algorithm] 배열 역순 정렬 (1) | 2023.12.05 |
[Algorithm] 재귀함수(피보나치 수열, 팩토리) (1) | 2023.11.27 |