简体   繁体   中英

How to find the odd and even numbers with if condition in java

String numbers=abc.nextLine(); numbers%2==0 i use this for odd numbers but how for the even numbers

You have it mixed up. Odd numbers are numbers that cannot be divided by 2. So numbers like 1,3,5,7. Numbers that are dividable by 2, are even numbers. So thats 2,4,6,8. To find an even number, you check whether the remainder of the modulus operator is 0 (as you have stated -> numbers % 2 == 0 -> even number). The opposite logic can be used for odd numbers. Basically if (numbers % 2,= 0). you have an even number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM