1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
4
Rep Power
0

#### Method Problem

i'm trying to print between x and y Harshad number is an integer that is divisible by the sum of its digits. However; the colored part is unreachable code in eclipse.

public void run () {

}

public int getDigit(int i) {
int result = 0;
while (true) {
result = i%10;
i /= 10;

}

return result;
}

public void printHarshadNumbers(int x, int y) {

for(int i=x;i<=y;i++) {
if(i%getDigit(i)==0) {
}

}

}
2. The while(true) loop in the getDigit(int) function never stops.

Maybe you can explain a bit further what you want/expect?

Please use code tags when posting code; this will keep the indention and make the code easier to read.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
4
Rep Power
0
Originally Posted by MrFujin
The while(true) loop in the getDigit(int) function never stops.

Maybe you can explain a bit further what you want/expect?

Please use code tags when posting code; this will keep the indention and make the code easier to read.
Firstly, thanks for reply. I want to print Harshad number between integer x and y. A Harshad number is an integer that is divisible by the sum of its digits. My solution is taking digit sum from a method and using it in other method to control that is it harshad number or not.
4. Originally Posted by intergalactic
My solution is taking digit sum
Where do you get the sum?

What do you expect from the while(true) loop? That it should run forever as it does now?
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
4
Rep Power
0
Originally Posted by MrFujin
Where do you get the sum?

What do you expect from the while(true) loop? That it should run forever as it does now?
i expect from the loop to get the sum of digits. I know there must be break condition to stop forever run, but i don't know how.

result ++; is in while loop of course.
6. Originally Posted by intergalactic
i expect from the loop to get the sum of digits. I know there must be break condition to stop forever run, but i don't know how.
You can have something like this to end the loop:
Code:
```if(condition)
break;```
.

But you have to define what the condition has to be to exit the loop.

Originally Posted by intergalactic
result ++; is in while loop of course.
You should try to include all part of the code that is relevant to the question.
Not sure why you use result ++;
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
4
Rep Power
0
Originally Posted by MrFujin
You can have something like this to end the loop:
Code:
```if(condition)
break;```
.

But you have to define what the condition has to be to exit the loop.

You should try to include all part of the code that is relevant to the question.
Not sure why you use result ++;

Code:
`if (i==0) break;`
and

Code:
`result += i%10;`
result ++; was for storage.

Finally, the code works, mucho gracias.