How to print ODD and EVEN elements from an array in java
The blog provides the technical approach on ” how to find the ODD and EVEN array elements in a one dimensional array”
Problem Statement:
Identify ODD and EVEN elements in one dimensional array
Solution Approach:
- Retrieve the Array Count from User Input
- Retrieve the Array Elements from User Input
- Iterate the array elements
- If array[i] %2 ==0, the array element is Even , else ODD
- Print the array elements using the above condition
package com.java.planforexams.array; import java.util.Scanner; public class OddEvenArrayElements { public static void main(String[] args) { // Scanner object for reading User Input Scanner scan = new Scanner(System.in); int count; // Receive the Array Element Count from User System.out.print("Provide the Array Element Count : "); // Get the Array Count count = scan.nextInt(); //Set the Array Size with Count value int arrayInputFromUser[] = new int[count]; // Receive the Array Elements from User. System.out.println("Enter the elements:"); // Iterate the array elements for (int incr=0; incr<count; incr++) { arrayInputFromUser[incr] = scan.nextInt(); } for(int incr = 0 ; incr < count ; incr++) { if(arrayInputFromUser[incr] % 2 == 0) { System.out.print(" \n Array Element is EVEN::: "+ arrayInputFromUser[incr]); } else { System.out.print(" \n Array Element is ODD::: "+ arrayInputFromUser[incr]); } } } } Output: Provide the Array Element Count : 4 Enter the elements: 12 11 34 33 Array Element is EVEN::: 12 Array Element is ODD::: 11 Array Element is EVEN::: 34 Array Element is ODD::: 33
Leave a Reply