Pages

Saturday, 18 March 2017

Even Odd Program in Java

Even Odd Program in Java

This java program finds if a number is odd or even. If the number is divisible by 2 then it will be even, otherwise it is odd. We use modulus operator to find remainder in our program. Odd Even Program in Java

Java programming source code

import java.util.Scanner;

class OddOrEvenDemo
{
   public static void main(String args[])
   {
      int x;
  System.out.println("Enter an integer to check if it is odd or even ");
  Scanner in = new Scanner(System.in);
  x = in.nextInt();

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

Output

Enter any Number: 2
This is an even number
Enter any Number: 31
This is an odd number

No comments:

Post a Comment