mmayoub

arrays-exercise 01, slide 28

Jul 1st, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. package class170629;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class arrays01 {
  6.  
  7.     public static void main(String[] args) {
  8.         /*
  9.          * Exercise 1, slide 28
  10.          * input : 10 integer numbers to an array
  11.          * output: even numbers only
  12.          */
  13.  
  14.         // the size of the array
  15.         final int SIZE = 10;
  16.  
  17.         // create a scanner
  18.         Scanner s = new Scanner(System.in);
  19.  
  20.         // define an integer array
  21.         int[] numbers = new int[SIZE];
  22.  
  23.         System.out.printf("Enter %d integer numbers:\n", numbers.length);
  24.         // get data from user and save it in the array
  25.         for (int i = 0; i < numbers.length; i += 1) {
  26.             numbers[i] = s.nextInt();
  27.         }
  28.         s.close();
  29.  
  30.         // check numbers and print event numbers only
  31.         for (int i = 0; i < numbers.length; i += 1) {
  32.             if (numbers[i] % 2 == 0)
  33.                 System.out.printf("%d ", numbers[i]);
  34.         }
  35.         System.out.println();
  36.  
  37.     }
  38.  
  39. }
Add Comment
Please, Sign In to add comment