public class Main {
/**
* Finds the smallest and largest digits in a number.
*
* @param number Input number
* @return An array where:
* result[0] = smallest digit
* result[1] = largest digit
*/
public static int[] findMinMaxDigits(int number) {
int minDigit = 9;
int maxDigit = 0;
// Handle negative numbers
number = Math.abs(number);
// Special case for 0
if (number == 0) {
return new int[]{0, 0};
}
while (number > 0) {
int digit = number % 10; // Extract the last digit
if (digit < minDigit) {
minDigit = digit;
}
if (digit > maxDigit) {
maxDigit = digit;
}
number /= 10; // Remove the last digit
}
return new int[]{minDigit, maxDigit};
}
public static void main(String[] args) {
int number = 839516;
int[] result = findMinMaxDigits(number);
System.out.println("Number: " + number);
System.out.println("Smallest digit: " + result[0]);
System.out.println("Largest digit: " + result[1]);
}
}
/*
run:
Number: 839516
Smallest digit: 1
Largest digit: 9
*/