Hi all,
I have encountered this problem of positive and negative numbers, odd and even, and I thought of this solution
if(number % 2 == 0 && number < 0) {
System.out.println("Negative even number");
}
if(number < 0 && number % 2 == 1) {
System.out.println("Negative odd number");
}
if(number % 2 == 0 && number > 0) {
System.out.println("Positive even number");
}
if(number % 2 == 1 && number > 0) {
System.out.println("Positive odd number");
}
if(number == 0) {
System.out.println("Zero");
}
that doesn't seem to work.
Any ideas, please?
Anonymous #10859127
Level 5
Odd_Even_Positive_Negative_Zero
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Jurij Thmsn
3 November 2021, 10:36
You are checking for the same conditions for even and odd, just in a different order.
Another thing to think about is the condition number % 2 == 1 in the negative odd number check.
Isn't this a question about a specific task? It is better if you use the Help section for the task because more people will see and respond to it.
0