Java program to find even or odd

Let’s learn java program to find even or odd.

Java program to find even or odd

Java program to check even or odd number is one of the common java program interview question.

In the below even odd program in java first user enters a number as input. This input number is divided by 2. For this we use modulus operator.

Also read – write a program to print even and odd numbers in java

If the remainder is equal to 0 then the number entered is even number else the number entered is odd number.

Now let’s see java program to find even or odd.

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:

Please enter a number to check even or odd : 5
Entered number is an odd number


Odd even program in java

Now let’s learn another method to check whether a number is even or odd. Similar to above java program user enters a number.

Also read – even odd program in java using array

Entered number is first divided by two and then multiplied by two. If the result is same as original number then number is even number, else odd number.

For example: entered number 18, 18/2 = 9 then 9*2 =18

import java.util.Scanner;

public class EvenOddDemo
{
   public static void main(String[] args)
   {
      int num;
      System.out.println("Please enter a number : ");
      Scanner sc = new Scanner(System.in);
      num = sc.nextInt(); 
      if((num/2)*2 == num)
      {
         System.out.println("Even number");
      }
      else
      {
         System.out.println("Odd number");
      }
      sc.close();
   }
}



Output:

java program to find even or odd

Using for loop

Now let’s learn even odd program in java using for loop. So to check if number is even or odd number, given number is divided by 2.

Also read – even odd program in java using ternary operator

Using modulus operator we have to check if remainder is zero. If the remainder is zero then given number is even number else given number is odd number.

Here’s the java code to display even or odd number from 1 To n or write a program to print even and odd numbers in java using for loop,

import java.util.Scanner;

public class EvenOddDemo 
{
   public static void main(String[] args)
   {
      int a;
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter number(max) to print all even and odd until: ");
      int number = sc.nextInt();
      System.out.print("Even numbers from 1 to " + number + " are: \n");  
      for(a = 1; a <= number; a++)
      {
         if(a%2 == 0)
         {
            System.out.print(a + "\n");
         }
      }
      System.out.print("Odd numbers from 1 to " + number + " are: \n");    
      for(a = 1; a <= number; a++)
      {
         if(a%2 == 1)
         {
            System.out.print(a + "\n");
         }
      }
      sc.close();
   }
}



Output:

Enter number(max) to print all even and odd until: 10
Even numbers from 1 to 10 are:
2
4
6
8
10
Odd numbers from 1 to 10 are:
1
3
5
7
9


Also read – even odd program in java using if else

Also read – even odd program in java using method

Also read – even odd program in java without using scanner

Also read – even odd program in java using while loop

Also read – java program to print odd and even numbers between 1 and 100


find odd or even number in java without using mod operator

Here’s how to find odd or even number in java without using mod operator,

public class EvenOddWithoutModulus
{
   public static void main(String[] args)
   {
      int number = 104;
      if(checkEvenNumber(number))
      {
         System.out.println(number + " is even number.");
      }
      else
      {
         System.out.println(number + "is odd number.");
      }
   }

   static boolean checkEvenNumber(int num)
   {
      boolean boolEven = true;
      for(int a = 1; a <= num; a++)
      {
         boolEven = !boolEven;
      }
      return boolEven;
   }
}


Output:

104 is even number.