Java Program To Find Even Or Odd

Hey guys!! Welcome to flower brackets blog. Today you will learn and execute java program to find even or odd.

java program to find even or odd

Here in the below java even or odd program modulus operator is used to find the number entered is even or odd.

Also Read – Add Two Numbers With Bitwise Operator

If the remainder of the modulus operator is equal to zero then the number entered is even number else the number entered is odd number.

Example: java program to find even or odd

Now let’s see an example on how to find even and odd numbers in java,

import java.util.Scanner;

public class EvenOdd
{
   public static void main(String[] args)
   {
      int a;
      System.out.println("Please enter a number to check even or odd:");
      Scanner sc = new Scanner(System.in);
      a = sc.nextInt();

      if(a % 2 == 0)
      {
         System.out.println("Entered number is an even number");
      }
      else
      {
         System.out.println("Entered number is an odd number");
      }
   }
}

Output:

Java Program To Find Even Or Odd


Print an array java

Here we are going to learn printing an array in java with an example,

import java.util.Scanner;

public class EvenOddArray
{
   public static void main(String[] args)
   {
      int number;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter number of elements you want in an array : ");
      number = sc.nextInt();
      int arrayNumber[] = new int[number];
      
      System.out.println("Please enter all the elements : ");
      for(int a = 0; a < number; a++) 
      {
         arrayNumber[a] = sc.nextInt();
      }
      System.out.print("Odd numbers in array : ");
      for(int a = 0 ; a < number ; a++)
      {
         if(arrayNumber[a] % 2 != 0)
         {
            System.out.print(arrayNumber[a] + " ");
         }
      }
      System.out.println("");
      System.out.print("Even numbers in array : ");
      for(int a = 0 ; a < number ; a++)
      {
         if(arrayNumber[a] % 2 == 0)
         {
            System.out.print(arrayNumber[a] + " ");
         }
      }
      sc.close();
   }
}

Output:

Please enter number of elements you want in an array : 10
Please enter all the elements : 1 2 3 4 5 6 7 8 9 10
Odd numbers in array : 1 3 5 7 9
Even numbers in array : 2 4 6 8 10


Using for loop

Let’s see an example on how to find even and odd numbers in java using for loop,

public class EvenOddForLoop
{
   public static void main(String[] args)
   {
      // creating an array of 10 numbers
      int[] arrayNumbers = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 
      for(int a = 0; a < arrayNumbers.length; a++)
      { 
         if(arrayNumbers[a] % 2 == 0)
            System.out.println(arrayNumbers[a] + " is an even number.");
         else
            System.out.println(arrayNumbers[a] + " is an odd number."); 
      }
   }
}

Output:

1 is an odd number.
2 is an even number.
3 is an odd number.
4 is an even number.
5 is an odd number.
6 is an even number.
7 is an odd number.
8 is an even number.
9 is an odd number.
10 is an even number.


Using conditional operator

Now let’s see a java program to check even or odd using conditional operator.

Conditional operator (ternary operator) is used instead of if and else.

public class EvenOddConditional
{
   public static void main(String[] args)
   {
      int num = 14;
      String result = num % 2 == 0 ? "Even" : "Odd";
      System.out.println(num + " is " + result);
   }
}

Output:

14 is Even


Using bitwise operator

Now let’s learn java program to find even and odd number using bitwise operator.

Here bitwise “AND” operator returns 1 if both operands are 1.

public class EvenOddBitwise
{
   public static void main(String[] args)
   {
      int number = 64;
      if((number & 1) == 1)
      {
         System.out.println(number + " is Odd.");
      }
      else
      {
         System.out.println(number + " is Even.");
      }
   }
}

Output:

64 is Even.


Without using modulus operator

Now we will learn java program to find even or odd without using mod operator,

public class WithoutModulus
{
   public static void main(String[] args)
   {
      int number = 73;
      if((number / 2) * 2 == number)
      {
         System.out.println(number + " is Even.");
      }
      else
      {
         System.out.println(number + " is Odd.");
      }
   }
}

Output:

73 is Odd.


conclusion

That’s it guys. This is all about how to find even and odd numbers in java. I hope you guys have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like