-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecimalTootherbase.java
More file actions
42 lines (34 loc) · 1.18 KB
/
DecimalTootherbase.java
File metadata and controls
42 lines (34 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// conert the decimal number to other base (2-36)
import java.util.Scanner;
public class DecimalTootherbase {
public static String convertToBase(int number, int base) {
if (base < 2 || base > 36) {
return "Base must be between 2 and 36.";
}
if (number == 0) {
return "0";
}
String result = "";
while (number > 0) {
int remainder = number % base;
char digit;
if (remainder < 10) {
digit = (char) ('0' + remainder);
} else {
digit = (char) ('A' + (remainder - 10));
}
result = digit + result;
number = number / base;
}
return result;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int number = scn.nextInt();
System.out.print("Enter the base to convert to (2-36): ");
int base = scn.nextInt();
String result = convertToBase(number, base);
System.out.println("Converted value: " + result);
}
}