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?

check the if condition..if the number is odd it will print weird and skip the else part. if the 'if' part proves false then by default the number is even ,it will enter the else loop to check for the limit condition and those numbers might be 6,8,10,12,14,16,18,20

## Java If-Else

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

import java.io.

; import java.util.; import java.text.; import java.math.; import java.util.regex.*;super

wow i love your reasonning

thanks jangapavanyadav

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

You don't need n%2==0. If it's between 6 and 20 inclusive it's Weird. Period. Doesn't matter if it's even or odd.

have you compiled this programme in this website complier?

worked pefectly!; thanks!!, will study this one more, still a beginner at this.

Can you explain us how you did this in simpler way? Thanks for being super!

The questions says n is even and and between 6 to 20 , by this logic even 7,9,11,13,15,17,19 will print "weird " .? Please expalin .?

you have to print odd numbers as well as even numbers in the range of 6 to 20 as weird. to find the even numbers you have to check if n%2==0

import java.io.

; import java.util.;It is correct.

Tq dude...

I recently joined a java development company, and this was very useful in my daily works. Thanks for sharing.

It is showing error for test case 4. Please help.

check the if condition..if the number is odd it will print weird and skip the else part. if the 'if' part proves false then by default the number is even ,it will enter the else loop to check for the limit condition and those numbers might be 6,8,10,12,14,16,18,20

where the second condition the number between 2 to 5 ?

thanks bro...!!

greAT