Reputation: 31
The task is to build a program in Java which where the user enter a base 10 number and when the selected numbering system is base 16, the program should replace the remainder numbers 10,11,12 by A,B and C respectively.
The part where I don’t understand is how to make the program replace the remainder number to letters.
Upvotes: 0
Views: 76
Reputation: 2307
Create a function that would do that. Since A, B, C are in order in ASCII table, you can use it for your advantage:
char c = 'A';
int charValue = number;
if (number > 9) charValue = (number - 10) + (int) c;
System.out.println((char) charValue);
So you take number, substract 10, add int value of first character 10 (so 10 will be A, 11 will be B, etc). This would be a basic idea for one reminder, full code depends on your logic for input (does it comes as string? would be better since you will end up with string, etc).
Upvotes: 1