Even odd program in java
Written by
Program to find even odd in Java
In the following question, we are supposed to ask the user for an integer input and then check if the input is an odd number or an even number.
The basic logic here goes as, if the number, when divided by 2, leaves 1 as remainder then it is an odd number otherwise it is an even number if the remainder is 0.
The standard algorithm will be:
- Enter and Store the Integer Input from the user.
- Declare an integer variable that will store the N modulus 2, i.e the remainder of the division.
- Using an If-Else construct, we’ll check if the remainder is 0 or 1.
- If remainder is 0, print “Even” else print”Odd”.
- Print the result accordingly.
Source Code:
/* Program to check if entered number is Even or Odd. */
import java.util.*;
class EvenOdd
{
public static void main()
{
Scanner inp=new Scanner(System.in);
System.out.print("\n Enter Number: ");
int n=inp.nextInt();
int a=n%2;
if(a==0)
System.out.println(n+" is an Even Number");
else
System.out.println(n+" is an Odd Number");
}
}
Output:
Enter Number: 2
2 is an Even Number
Enter Number: 365
365 is an Odd Number
Enter Number: 420
420 is an Even Number