Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /* ==================================================
- Chapter 2: Syntax section C- Arrays
- Ex1: Display only even values in array
- ===================================================
- */
- public class MyProgram {
- public static void main(String[] args) {
- //variables
- final int SIZE = 10;
- int arr[] = new int[SIZE];
- Scanner s = new Scanner(System.in);
- //instructions for user
- System.out.println("Enter 10 numbers:\npress Enter only AFTER 10 values!" +
- "\nseparate numbers with a gap\n");
- System.out.println("the values are: ");
- //loop until user input completed
- for (int i = 0; i < SIZE; i++) {
- //init array values
- arr[i] = s.nextInt();
- //print only even values in array
- if (arr[i] % 2 == 0) {
- System.out.print(arr[i] + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment