# How to Find Odd and Even Numbers In Java?

What We Are Learn:

In Today’s blog post, we’ll take a practical approach and see a practical program in Java. The question for today is to write a program to find Odd and Even numbers In Java. For this tutorial, we’ll take a number and determine whether it is an Odd or Even number.

So we have to decide here, which means we will use the conditional statement, the If Else statement. So I’m going to just hard code the number, which means I will declare it in the program. we’ll declare an integer variable. I’ll say X =5. Now, we have to check whether this number is even or odd.

So, how do we implement the logic in terms of programming or mathematics? Well, the way we implement this is we divide this number by 2, and if you are getting any remainder, it means that it is not divisible by 2, which will mean that it is an odd number, right if we can properly divide that number and get a remainder of 0 then we can say that it is an even number right.

So let’s say 4 divided by 2 will give you 0, and 3 divided by 2 will not give you zero as they remained up it would give you one, of course. We are talking about whole numbers and not fractional numbers, so in that case, when we divide an odd number, we always remain with the remainder of 1, so this logic we can use in terms of programming as well.

So, we are going to use the modulo operator. That’s why I’m going to create one more variable. I’ll say int remainder, and I’m going to say remainder=X%2. Here, this % sign is the modulo operator, and what it does is it gives us the remainder by performing division, so 5 divided by 2 will give you a remainder of 1. That one will be stored in the remainder variable we’ve created because we know that the assignment happens from right to left.

### Odd and Even Numbers

So, whatever the result of this expression is, it will be transferred to the remainder variable. After that, we have to make a decision by comparing what we get in this remainder variable. so to make a decision, we use the if-else conditional statements. I’ll say if remainder== 1, that means we are getting an odd number; otherwise, I will say an even number.

If I don’t get one as the remainder, the only other possibility is 0, right? The number will be either even or odd. and if it is odd, we have to get a remainder of 1; if it is even, the remainder will always be 0.

```package com.softwaretestingo.basic;
public class OddandEvenNumber
{
public static void main(String[] args)
{
int x=5;
// 4/2-0
// 3/2-1
int reminder=x%2;
if(reminder==1)
{
System.out.println("Odd number");
}
else
{
System.out.println("Even number");
}
}
}```
`Odd Number`

Let me save this and try to run this program; there you go; you can see we got the output as an odd number. the reason why we got an odd number is that our variable X is 5. Here, if you notice, we have hard-codded the value, but you can also use the scanner class to take the input from the user in the run time.

### Find Odd and Even Numbers 1 to 100.

Here’s an example of how to display the Odd and Even Numbers 1 to 100. The value of ‘n’ is set at 100, meaning this program will print all the odd and even numbers from 1 to 100.

If you are struggling to understand this program, you should review your knowledge of for loops in Java and if statements. These concepts will help you grasp the program more easily.

```package com.softwaretestingo.basic;
public class OddandEvenNumbers1to100
{
public static void main(String[] args)
{
int n = 100;
System.out.print("Even Numbers from 1 to "+n+" are: ");
for (int i = 1; i <= n; i++)
{
//if number%2 == 0 it means its an even number
if (i % 2 == 0)
{
System.out.print(i + " ");
}
}
}
}```

Conclusion:

So that’s all for the Odd and Even numbers In Java. I hope you understand this program. This was a very simple program wherein we had to decide using the if-else conditional statements, and if you liked this tutorial give it a thumbs up and share it with your friends.

If still, you have any doubts or confusion about the Odd and Even numbers program, then let us know in the comment section and we will be happy to clarify your doubts. I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.