In programming, it's common to work with arrays to store a collection of data. One common task is to count how many numbers in an array are even and how many are odd. In Java, this can be done easily using simple loops and conditional statements. In this article, we'll explore how to write a Java program that counts the number of even and odd numbers in an array.
- Even Numbers: These are numbers that are divisible by 2 without leaving a remainder. For example, 2, 4, 6, 8, etc., are even numbers.
- Odd Numbers: These are numbers that are not divisible by 2. They leave a remainder of 1 when divided by 2. Examples include 1, 3, 5, 7, etc.
To count the number of even and odd numbers in an array.
- Iterate through each element of the array.
- For each element, check if it's divisible by 2.
- If it's divisible by 2 (i.e., num % 2 == 0), it's an even number.
- If it's not divisible by 2, it is an odd number.
- Maintain two counters: one for even numbers and one for odd numbers.
- At the end, output the count of even and odd numbers.
Java Code Implementation
Here’s how you can implement the solution in Java.
import java.util.Scanner;
public class EvenOddCounter {
public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner scanner = new Scanner(System.in);
// Ask the user to enter the number of elements in the array
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
// Initialize the array to store the numbers
int[] numbers = new int[n];
// Take input from the user to fill the array
System.out.println("Enter the numbers in the array:");
for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
}
// Variables to store the count of even and odd numbers
int evenCount = 0;
int oddCount = 0;
// Loop through the array and check each number
for (int num : numbers) {
if (num % 2 == 0) {
evenCount++; // Increment even count if the number is even
} else {
oddCount++; // Increment odd count if the number is odd
}
}
// Display the results
System.out.println("Number of Even numbers: " + evenCount);
System.out.println("Number of Odd numbers: " + oddCount);
// Close the scanner object
scanner.close();
}
}
Explanation of the Code
- Input Handling
- We use the Scanner class to take input from the user. The user first enters the size of the array and then provides the numbers to populate the array.
- Even and Odd Counting
- The program uses a loop to iterate through each element of the array. For each number, it checks if the number is divisible by 2 (num % 2 == 0).
- If true, the number is even, and the evenCount variable is incremented. Otherwise, the oddCount variable is incremented.
- Output
- After the loop finishes, the program prints the number of even and odd numbers in the array.
Example Run
Let’s consider an example where the user inputs an array of numbers.
Input
Enter the number of elements in the array: 6. Enter the numbers in the array: 1 2 3 4 5 6
Output
In this case, the program successfully counted 3 even numbers (2, 4, 6) and 3 odd numbers (1, 3, 5).
Conclusion
Counting even and odd numbers in an array is a straightforward task in Java. By iterating through the array and using the modulus operator (%
), we can easily classify numbers as even or odd. This simple approach provides a solid understanding of loops, conditional statements, and array handling in Java.