We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

you dont need to add (n%2==0) in
if(n%2==1 || ( (n%2==0) && (n>=6 && n <= 20 ) ) ) statement
as it is not to checking whether the number is even.
we need to check only that number is odd so (n%2==1) is important and number is in bitween 6 to 20.
if this doesnot follow go to direct else statement.
i ran without (n%2==0) statement and it ran perfectly.

are you kidding :-p ,
If n is even and in the inclusive range of 6 to 20, print Weird,
so you didn't check for the even condition and if nis in the range or 6 to 20 you are printing wired, which is wrong

No i agree with you that it should check the even condition but
i dont know what is wrong with the hackerank testing arlgorithm that it is only considering number between 6 to 20(irrespective of even or odd).Go ahead submit the code without n%2==0 condition it does follow all test cases correctly.Thats why i said code is not asking you to check even condition.

Well even I added n%2==0 condition but we do not need to.
As it has been said that if n is odd it is weird and if even numbers between 6-20 are weird then it doesnt matter that the number is even or odd in 6-20 as odd numbers are already weird with the condition n%2==1.
So we can have if(n%2==1 || (n>=6 && n<=20)) for the weird condition.

|| is a short hand operator. If the left side is evaluated to true, it doesnt even evaluate the right side expression. If a number is odd, the right side expression isnt even evaluated - if the number isnt odd, then it must be even right? why bother adding n%2==0 again?

## Java If-Else

You are viewing a single comment's thread. Return to all comments →

you dont need to add (n%2==0) in if(n%2==1 || ( (n%2==0) && (n>=6 && n <= 20 ) ) ) statement as it is not to checking whether the number is even. we need to check only that number is odd so (n%2==1) is important and number is in bitween 6 to 20. if this doesnot follow go to direct else statement. i ran without (n%2==0) statement and it ran perfectly.

are you kidding :-p , If n is even and in the inclusive range of 6 to 20, print Weird, so you didn't check for the even condition and if nis in the range or 6 to 20 you are printing wired, which is wrong

No i agree with you that it should check the even condition but i dont know what is wrong with the hackerank testing arlgorithm that it is only considering number between 6 to 20(irrespective of even or odd).Go ahead submit the code without n%2==0 condition it does follow all test cases correctly.Thats why i said code is not asking you to check even condition.

Well even I added n%2==0 condition but we do not need to. As it has been said that if n is odd it is weird and if even numbers between 6-20 are weird then it doesnt matter that the number is even or odd in 6-20 as odd numbers are already weird with the condition n%2==1. So we can have if(n%2==1 || (n>=6 && n<=20)) for the weird condition.

|| is a short hand operator. If the left side is evaluated to true, it doesnt even evaluate the right side expression. If a number is odd, the right side expression isnt even evaluated - if the number isnt odd, then it must be even right? why bother adding n%2==0 again?

EXACTLY