Let’s see an interesting program for school/college students who are learning core Java.
To find whether an entered number is Odd or Even is very simple, all you have to do is divide the number by 2 and if you get the remainder 0 (zero) then it is an Even number or it is an Odd number.
Eg: 10 / 2 = 5, here 5 is the quotient, 0 is the remainder.
In Java there are two operators you can use to find quotient and remainder. (/) slash and (%) modulus, where the / will give the quotient and % will give the remainder value.
So here is the Java Program to find Odd or Even number!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/** * @author agur */ import java.util.Scanner; public class OddorEven { public static void main(String[] args) { int num; Scanner in = new Scanner(System.in); System.out.println("Enter a number:"); //get the input value num = in.nextInt(); //if the value of c is 2 then it is a prime number //because a prime number should be exactly divisible by 2 times only (itself and 1) if ( num % 2 == 0) { System.out.println(num+" is an Even number!"); } else { System.out.println(num+" is an Odd number!"); } } } |
Just read the comments in the above program to understand it.
Example Output of the above program:
1 2 3 |
Enter a number: 15 15 is an Odd number! |
Enjoy Learning!