Count even and odd number in an Integer.

In this program we will Count even and odd number in an Integer.

Using Modulo Operator :-

  • First we initialize a number value.
  • Take two variables, evenCount=0, oddCount=0.
  • Iterate the number from while loop.
  • Find reminder of number using modulo operator.
  • Increment evenCount if(arr[i]%2==0)
  • Otherwise increment the value of oddCount by 1.
				
					public class EventOddCountNumber {
	public static void main(String[] args) {
		int number = 12345;
		int evenCount = 0, oddCount = 0;
		while (number > 0) {
			int rem = number % 10;
			if (rem % 2 == 0) {
				evenCount++;
			} else {
				oddCount++;
			}
			number = number / 10;
		}
		System.out.println("Even number are :- " + evenCount);
		System.out.println("Odd number are :- " + oddCount);
	}
}
				
			

Output :-
Even number are :- 2
Odd number are :- 3