For example, what would be the most efficient way to get say 999 if given an n that equals 3 for instance.
This is what I have got right now but I was wondering if there was a more elegant way.
public static int largestPossibleNumber(int numDigits) {
return Integer.parseInt(new String(new char[numDigits]).replace("\0", "9"));
}
Example Usage:
for (int i = 1; i <= 5; i++) {
System.out.println(largestPossibleNumber(i));
}
Output:
9
99
999
9999
99999