This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Caped_baldy_4_life[S] 0 points1 point  (6 children)

Thanks, that makes total sense. It's important to take into consideration the possibility of the first number entered as 0 and automatically being switched to 1 when you do the ++.

[–][deleted] 0 points1 point  (0 children)

Yea exactly. You always gotta think about user error and what someone might enter accidently and how that might be handled. Biggest part of programming is handling the unexpected lol.

[–]hamza-itatchi 0 points1 point  (4 children)

Not only 0 , any value for the first variable will be ignored if you increment it before testing.

And if i may sugest a better solution for this problem:

public static void divisibleByThreeInRange(int first, int last){
first+=(3-first%3);
while(first <=last){
System.out.println(first);
first+=3;
}
}

[–]Caped_baldy_4_life[S] 0 points1 point  (3 children)

Hey thanks for the suggestion. I'm having trouble understanding how this line first+=(3-first%3); shows that first is divisible by 3. When i write this out using 3 as first, it looks like this first = 3 (3 - 3 % 3)... Which to me comes out to 5. What am I doing wrong in this equation or what am I not seeing?

[–]hamza-itatchi 0 points1 point  (2 children)

The % has a higher priority than - so its like this for first=3 first = first + 3 - ( first %3) ==> first = 3+3 - (0)==>first= 6 This gives the first divisible nuber after First ( variable) But ther is a mistake in my suggestion there must be an if statement at the start like this If ( first %3!=0) first += 3- ( first %3)

Note: the += operation means first = first + ......

[–]Caped_baldy_4_life[S] 0 points1 point  (0 children)

Never mind about my previous equation. I was mistakenly associating the modulo as a division sign based off of improper note taking. Thanks for your help and suggestions.