Even Odd Number Example /*   Even Odd Number Example   This Java Even Odd Number Example shows how to check if the given...

Even Odd Number Example

23:58 PASSOVE INCOME 0 Comments

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

0 comments: