# Odd Number Program in Java

Odd Number Program in Java | Find Odd Number in Java | Previously we had developed a Java program to check number is even or not. Now, we develop a Java program to check whether the number is an odd number or not. If the number is not divisible by 2 then the number is called an odd number.

``````import java.util.Scanner;

public class CheckOdd {

public static boolean isOdd(int n) {
// if number is not divisible by
// 2 then return true
if(n % 2 != 0) return true;

// else return false
return false;
}

public static void main(String[] args) {

// declare variable
int number = 0;
boolean result = false;

// create Scanner class object to take input
Scanner scan = new Scanner(System.in);

System.out.print("Enter an integer number:: ");
number = scan.nextInt();

// check number is odd or not
result = isOdd(number);

// display result
if(result)
System.out.println(number+
" is an odd number");
else
System.out.println(number+
" is not an odd number");

// close Scanner class object
scan.close();
}
} ``````

Output for Case test-1:-

Enter an integer number:: 9
9 is an odd number

Output for Case test-2:-

Enter an integer number:: 10
10 is not an odd number

## Different ways to find an odd number in Java

### Using a conditional operator

Similarly to the if-else statement, we can also use the ternary/conditional operator in Java to check a number is an odd number or not.

``````public static boolean isOdd(int n) {
return (n % 2 != 0) ? true : false;
}``````

It is similar to the if-else statement. The if-else statement takes more than one line of the statements, but the conditional operator finishes the same task in a single statement. The conditional operator in C is also called the ternary operator because it operates on three operands.

For the ternary operator:- In the given below expression,

``(expression1) ? expression2 : expression3;``

If the expression1 is true then it returns the result of expression2 else it returns the result of expression3. Here if the number is divisible by 2 then `true` will be returned to the main method else `false` will be returned to the main method.

### Odd number program in Java using switch case

``````public static boolean isOdd(int n) {
int option = n % 2;
switch(option) {
case 0:
return false;
case 1:
return true;
}
return false;
} ``````

The switch case statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly.

## Program to print odd numbers in Java using a loop

Similar to the even numbers we can also print odd numbers in Java within a range. For this purpose, we need to take the help of a loop. You can use any one way to check the number is odd or not, but we used the ternary operator.

``````import java.util.Scanner;

public class OddInRange {

public static boolean isOdd(int n) {
return (n % 2 != 0) ? true : false;
}

public static void main(String[] args) {

// declare variable
int minRange = 0, maxRange = 0;
boolean result = false;

// create Scanner class object to take input
Scanner scan = new Scanner(System.in);

System.out.print("Enter min range:: ");
minRange = scan.nextInt();
System.out.print("Enter max range:: ");
maxRange = scan.nextInt();

// check number is odd or not
System.out.println("Odd numbers are: ");
for(int i = minRange; i <= maxRange; i++) {
result = isOdd(i);
// display result
if(result)
System.out.print( i +" ");
}

// close Scanner class object
scan.close();
}
} ``````

Output:-

Enter min range:: 10
Enter max range:: 25
Odd numbers are:
`11 13 15 17 19 21 23 25 `

In this program, we have used for loop. The for loop is a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!