Odd Number Java

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

0 comments: