Write a Java program to find the count of even numbers in a sequence using varargs
The class contains a method called countEvenNumbers, which takes a variable number of integers as input. Inside this method, it iterates through each number in the input array and checks if it's even (divisible by 2 with no remainder). If a number is even, it increments a counter.
The main method is the entry point of the program. Inside main, it calls the countEvenNumbers method with a list of numbers from 1 to 10 as arguments. It stores the result, which is the count of even numbers, in a variable named count.
Finally, it prints out the result using System.out.println, displaying "Count of Even Numbers : " followed by the calculated count of even numbers.
public class EvenNumberCounter { static int countEvenNumbers(int... numbers) { int count = 0; for (int num : numbers) { if (num % 2 == 0) { count++; } } return count; } public static void main(String[] args) { int count = countEvenNumbers(1, 2, 3, 4, 5, 6, 7, 8, 9 ,10); System.out.println("Count of Even Numbers : " + count); } }
Count of Even Numbers : 5
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions