Advertisements
How to check given number is even or odd?
Simple if it is devided by 2 it is even and if not if is Odd
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class EvenNumber{ public static void main(String args[]) throws IOException { Sys tem.out.println("Enter the number"); int num= 0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String data = br.readLine(); num = Integer.parseInt(data); if((num %2)==0){ System.out.println("Number is Even"); } else{ System.out.println("Number is Odd"); } } }
Description: num is variable which hold the inputted number. BufferReader is used for inputting number. Even numbers are those numbers, which are divisible by 2, and odd numbers are not divisible by 2. if((num %2)==0) is checking the condition of num divisibility. If divisible then it will print even number else odd number.
/* Even Odd Number Example This Java Even Odd Number Example shows how to check if the given number is even or odd. */ public class FindEvenOrOddNumber { public static void main(String[] args) { //create an array of 10 numbers int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10}; for(int i=0; i < numbers.length; i++){ /* * use modulus operator to check if the number is even or odd. * If we divide any number by 2 and reminder is 0 then the number is * even, otherwise it is odd. */ if(numbers[i]%2 == 0) System.out.println(numbers[i] + " is even number."); else System.out.println(numbers[i] + " is odd number."); } } } /* Output of the program would be<br> 1 is odd number.<br> 2 is even number.<br> 3 is odd number.<br> 4 is even number.<br> 5 is odd number.<br> 6 is even number.<br> 7 is odd number.<br> 8 is even number.<br> 9 is odd number.<br> 10 is even number.<br> */