Even Odd Number Example

  1. /*
  2.   Even Odd Number Example
  3.   This Java Even Odd Number Example shows how to check if the given
  4.   number is even or odd.
  5. */
  6.  
  7. public class FindEvenOrOddNumber {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. //create an array of 10 numbers
  12. int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};
  13.  
  14. for(int i=0; i < numbers.length; i++){
  15.  
  16. /*
  17. * use modulus operator to check if the number is even or odd.
  18. * If we divide any number by 2 and reminder is 0 then the number is
  19. * even, otherwise it is odd.
  20. */
  21.  
  22. if(numbers[i]%2 == 0)
  23. System.out.println(numbers[i] + " is even number.");
  24. else
  25. System.out.println(numbers[i] + " is odd number.");
  26.  
  27. }
  28.  
  29. }
  30. }
  31.  
  32. /*
  33. Output of the program would be
  34. 1 is odd number.
  35. 2 is even number.
  36. 3 is odd number.
  37. 4 is even number.
  38. 5 is odd number.
  39. 6 is even number.
  40. 7 is odd number.
  41. 8 is even number.
  42. 9 is odd number.
  43. 10 is even number.
  44. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!