# Even odd program in java using ternary operator

Let’s learn even odd program in java using ternary operator.

## Even odd program in java using ternary operator

Write a program to print even and odd numbers in java is one of the frequently asked interview question.

In the below java program to check whether the given number is even or odd first user enters a number using nextInt() method of Scanner class.

This number is stored in integer variable ‘number’.

Then if the given number is divisible by 2, then the number is even.

Else the number is odd. Final result is stored in string variable “strOutput”.

Also read – even odd program in java using for loop

Let’s write a program on even odd program in java using ternary operator.

```import java.util.Scanner;
public class EvenOddTernary
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number : ");
int number = sc.nextInt();
// java odd or even
String strOutput = (number % 2 == 0) ? "even" : "odd";
System.out.println(number + " is " + strInput);
sc.close();
}
}

```

Output:

Please enter a number : 15
15 is odd

Even odd program in java using if else

Meanwhile we can check if an integer is odd or even using if else statement.

Here in the below java program first user enters a number.

This number is stored in integer variable ‘number’ using nextInt() method of Scanner class.

To check whether number is even or odd, we divide the number by 2 and calculate its remainder using % operator.

To check the condition we use if else statement in java.

If the given number is divisible by 2 then it is even number else odd number. Here’s the code,

```import java.util.Scanner;
public class EvenOddIfElse
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number : ");
int number = sc.nextInt();
if(number % 2 == 0)
{
System.out.println(number + " is even");
}
else
{
System.out.println(number + " is odd");
}
sc.close();
}
}

```

Output:

Please enter a number : 66
66 is even

Even odd program in java using method

Let’s learn even odd program in java using method.

```import java.util.Scanner;
public class EvenOddUsingMethod
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the number to check whether given number is odd or even: ");
int number = sc.nextInt();
checkOddEven(number);
sc.close();
}
static void checkOddEven(int num)
{
if(num%2 == 0)
{
System.out.println(num + " is even number.");
}
else
{
System.out.println(num + " is odd number.");
}
}
}

```

Output:

Please enter the number to check whether given number is odd or even: 62
62 is even number.

### even odd program using while loop

Here’s even odd program in java using while loop.

```import java.util.Scanner;
public class EvenOddUsingWhileLoop
{
public static void main(String[] args)
{
int a;
Scanner sc = new Scanner(System.in);
System.out.println("Even odd numbers in java until: \n");
int number = sc.nextInt();
a = 1;
System.out.println("Even numbers from " + a + " to " + number + " are: \n");
while(a <= number)
{
if(a % 2 == 0)
{
System.out.print(a + "\n");
}
a++;
}
a = 1;
System.out.println("Odd numbers from " + a + " to " + number + " are: \n");
while(a <= number)
{
if(a % 2 == 1)
{
System.out.print(a + "\n");
}
a++;
}
sc.close();
}
}

```

Output:

Even odd numbers in java until: 20
Even numbers from 1 to 20 are:
2
4
6
8
10
12
14
16
18
20
Odd numbers from 1 to 20 are:
1
3
5
7
9
11
13
15
17
19

Even odd program in java without using scanner

Let’s learn even odd program in java without scanner.

```public class EvenOddWithoutScanner
{
public static void main(String[] args)
{
int number = 95;
if(number % 2 == 0)
{
System.out.println(number + " is even number.");
}
else
{
System.out.println(number + " is odd number.");
}
}
}

```

Output:

95 is odd number.

odd or even using switch case in java

Let’s learn odd or even using switch case in java.

```public class EvenOddUsingSwitchCase
{
public static void main(String[] args)
{
int number1 = 629;
switch(number1 % 2)
{
case 0:
System.out.println(number1 + " is an even number.");
break;
case 1:
System.out.println(number1 + " is an odd number.");
}
}
}

```

Output:

629 is an Odd number