# less20 java challenge

Hi,
I am trying below challenge

http://codingbat.com/prob/p133158

I tried as  below
``````public boolean less20(int n) {
if(n%20==1||n%20==2){

return true;
}

else
return false;
}
``````

I am getting below result

Expected      Run
less20(18) → true      false      X
less20(19) → true      false      X
less20(20) → false      false      OK
less20(8) → false      false      OK
less20(17) → false      false      OK
less20(23) → false      false      OK
less20(25) → false      false      OK
less20(30) → false      false      OK
less20(31) → false      false      OK
less20(58) → true      false      X
less20(59) → true      false      X
less20(60) → false      false      OK
less20(61) → false      true      X
less20(62) → false      true      X
less20(1017) → false      false      OK
less20(1018) → true      false      X
less20(1019) → true      false      X
less20(1020) → false      false      OK
less20(1021) → false      true      X
less20(1022) → false      true      X
less20(1023) → false      false      OK
less20(37) → false      false      OK
other tests
X

how to fix that and improve my approach and design of this challenge. How do i make a graphical venn or ascii diagram to design it before writing single line of code to decide best strategy?
LVL 7
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You are checking for n%20 being 1 or 2, but you want them to be -1or-2 .
Commented:
Here's one way to solve it:
``````public boolean less20(int n) {
if((n+20)%20>=18){

return true;
}

else
return false;
}
``````
Commented:
return n%20>=18;

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
n%20>=18 works!
Author Commented:
How to use mod method approach
Commented:
return n%20==20-1 || n%20==20-2;  // return n%20>=18;
Author Commented:
i though mod means which covers either negative or positive but looks like it is reminder
Commented:
% in Java only works as a modulo operator for non-negative numbers,
but in this challenge, you are given a non-negative number, so it's ok to use it here.
Author Commented:
how to use Match.mod() here?

Commented:
The codingbat.com site does not seem to support the Java8 function Math.floorMod(n,20)
but you can emulate it with (n%20+20)%20
which is unnecessary in this case since you know that you are given a non-negative number
Author Commented:
``````public boolean less20(int n) {
if(n%20==19||n%20==18){

return true;
}

else
return false;
}
``````

above also passed all tests similar to other solution
Author Commented:
You are checking for n%20 being 1 or 2, but you want them to be -1or-2 .

how to check -1 and -2 for this challenge?
Commented:

to  if(n%20==-1||n%20==-2){
Commented:
No that's wrong sorry. Font is too tiny on my phone and I can't find a way to delete my prev comment.
Author Commented:
yes i tried as above but wont work
Commented:
Your comment ID: 41371808 looks like the correct solution.
A number mod 20 will return any value less than 20. The remainder after dividing by 20.
So your test for 18 OR 19 will be true if the number is 1 OR 2 less than a multiple of twenty.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.