In Today’s blog post we’re gonna 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’re gonna take a number and find out whether it is an Odd and Even number.
So we have to make a decision over here which means that we are going to be using the conditional statement which is the If Else statement. so what I’m going to do is I’m just gonna hard code the number which means I’m gonna declare it in the program. we’ll just declare an integer variable I’ll say int 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 in 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 and if we are able to properly divide that number and get a remainder of 0 then we can say that it is an even number right.
Post Type: | Core Java Tutorial, Java Programs Tutorial |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | Join Our Telegram Group |
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 what I’m going to do I am going to create one more variable I’ll say int remainder and I’m going to say remainder=X%2. Here this % sign is actually 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. And that one will be stored in this remainder variable that 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 going to be transferred to this remainder variable. After that, we have to make a decision by comparing what did 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 if we are getting an odd number right else what I’m going to say is an even number.
Because if I don’t get one as the remainder the only other possibility is going to be 0 right because the number will be either even or odd. and if it is odd we gotta get a remainder of 1 and if it is even the remainder is always going to 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 just save this and try to run this program, there you go and 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 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 understood this program this was a very simple program wherein we just have to make a decision 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 happy to clarify your doubts.
Leave a Reply