Ads

How to check given number is even or odd?

Ads

View Answers

May 23, 2012 at 5:51 PM

Simple if it is devided by 2 it is even and if not if is Odd

May 23, 2012 at 5:53 PM

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.

May 25, 2012 at 12:37 AM

/* 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> */

Ads

Related Tutorials/Questions & Answers:

Ads

- Java Tutorials
- Java Code example
- Java Programming
- Java Beginners Examples
- Applet Tutorials
- Awt Tutorials
- Java Certification
- Interview Question
- Java Servlets Tutorial
- Jsp Tutorials
- Java Swing Tutorials
- JDBC Tutorial
- EJB Tutorials
- Java Server Faces (JSF) Tutorial
- WAP Tutorial
- Struts Tutorial
- JAXB Tutorial
- Spring FrameWork Tutorial
- SOA&Web Services Tutorials
- Bioinformatics Tutorials
- MySQL Tutorials
- JAVA DOM Tutorial
- XML Tutorial
- EAI Articles
- Many Programming Tutorials Links
- Tutorials Books
**Java Script Tutorial****Ajax Tutorial****Dojo Tutorials****Programming Books****Trainings****Flex****Ant****RDF**

Ads