This program segment given below uses a straight-forward approach to count the number of odd and even digits in a given integer number(num).
Initially, variables nodd and neven, the counts for odd and even digits, respectively, are initialized to zero. Then a while loop is used to count the odd and even digits. In each iteration of this loop, the least significant digit of number num is first determined in variable digit,then the appropriate counter is incremented using an if-else statement and the least significant digit is removed from number num. The loop is executed as long as the value of num is greater than zero, i. e., all digits in num are not processed. Finally, the values of nodd and never are printed.
int nodd,neven,num,digit ;
printf("Count number of odd and even digits in a given integer number ");
nodd = neven =0; /* count of odd and even digits */
while (num> 0)
digit = num % 10; /* separate LS digit from number */
if (digit % 2 == 1)
num /= 10; /* remove LS digit from num */
printf("Odd digits : %d Even digits: %d\n", nodd, neven);