Write a method named digitRange that accepts an integer as a parameter and returns the range of values of its digits. The range is defined as 1 more than the difference between the largest and smallest =digit value. For example, the call of digitRange(68437) would return 6 because the largest digit value is 8 and the smallest is 3, so 8 -3+1=6. If the number contains only one digit, return 1. You should solve this problem without using a String.
// package indefinite_loops;
public class DigitRange {
public static int digitRange(int number) {
// get the abosolute value of the entered number using Math class
number = Math.abs(number);
// assign max and min values
int maxValue = number % 10;
int minValue = number % 10;
number /= 10;
// loop continous as long as number greater than zero
while(number > 0) {
int digit = number % 10;
if(digit > maxValue) {
maxValue = digit;
} else if(digit < minValue) {
minValue = digit;
}
number /= 10;
}
return maxValue - minValue + 1;
}
public static void main(String[] args) {
int range = digitRange(6);
System.out.println("the range of values of it's digits is: " +range);
}
}
Output:
the range of values of it's digits is: 5