Lab- Loops
Harshad or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits. For example, 42 is a Harshad number as 42 is divisible by (4 + 2) without remainder.
Using the code shown below, complete the loops to print the Harshad numbers in the range of 1 to 1000.
public class Harshad {
public static void main(String[] args) { int max = 1000;
for (int i = 1; i < max; i++) {
// Loop to sum the digits of i int j = i, sum = 0;
while ( . . . ) { // Complete the loop condition
// Add logic to compute the sum of the digits
}
// Check if this is a Harshad number
if ( . . . ) { // Complete the if statement System.out.println(i + " is a Harshad number");
}
}
}
}
Hint: the modulus operator can be used to extract single digits.