Complete the loops to print the harshad numbers in the


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.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Complete the loops to print the harshad numbers in the
Reference No:- TGS02384296

Now Priced at $10 (50% Discount)

Recommended (94%)

Rated (4.6/5)