
/* hw4p2 */

#include <stdio.h>
#include "simpio.h"
#include "genlib.h"

bool HasEvenDigit(int n);

void main() {
  int n;

  printf("Enter an integer: ");
  n = GetInteger();
  if (HasEvenDigit(n))
    printf("%d has an even digit\n", n);
  else
    printf("%d does not have an even digit\n", n);
}

/* uses the program from p.122 as a way to pick off the digits of n one
at a time to check whether each one is even or odd.  A slight bug is that
if n==0, the while loop will never be entered, and so the function will
return FALSE (if the first "if" statement wasn't there.)  Where as if the
number is, say, 10, then the function will first examine 1, and then 
examine 0, returning TRUE since 0 is divisible by 2.

This didn't matter for the program in the book, since it was just summing
up the digits, but it does matter here.  So the first "if" statement is
just a check for the special case of n==0.  Not too elegant!

*/

bool HasEvenDigit(int n)
{
  int digit;

  if (n==0)    /* a hack to check for the special case of n==0 */
    return TRUE;
  while (n > 0) {
    digit = n % 10;  /* pick off a digit of n each time through the loop */
    n = n / 10;
    if ((digit % 2) == 0)  /* digit is even */
      return TRUE;
  }
  return FALSE;      /* did not find an even digit in n */
}
