Algorithm Analysis
Separate cases should be handled when the input number is negative or equals zero. Then, we iterate through the digits of the number to find the product of even digits. A separate case should also be considered if none of the digits of the number are even.
Algorithm Implementation
Read the input number n
. If it is negative, then set its value to the opposite. If n
= 0, then the answer is 0.
scanf("%lld", &n); if (n < 0) n = -n; if (n == 0) res = 0; else res = 1;
Loop through the digits of the number n
. Find the product of even digits in the variable res
.
while(n) { if (n % 2 == 0) res *= (n % 10); n /= 10; }
If res
= 1, then the input number does not have any even digits, output -1.
if (res == 1) res = -1;
Output the answer.
printf("%lld\n", res);