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

Time:09-17

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

CodePudding user response:

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.

  • Related