Program to verify if odd numbers are equal with even numbers


A natural number x of maximum 9 digits is read from the keyboard. It is required to check whether the number of even digits is equal to the number of odd digits.
Example: if x = 61432, then it has 3 even digits (2, 4, 6) and 2 odd digits (3, 1), from which it results that the number of even digits is not equal to the number of odd digits. Make this with while and without subprograms please :*
Use operators / and % (in the loop) to extract digits.

Integer division (/) discards the fraction. For example 309 / 10 == 30
Modulus (%) gives that fraction. For example 309 % 10 == 9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
	size_t num {}, cnt[2] {};

	std::cout << "Enter number: ";
	std::cin >> num;

	do
	{
		++cnt[(num % 10) % 2];
	} while (num /= 10);

	std::cout << (cnt[0] == cnt[1] ? "Equal\n" : "Unequal\n");
}

Topic archived. No new replies allowed.