457 % 10 = 7 *
457 / 10 = 45
45 % 10 = 5 *
45 / 10 = 4
4 % 10 = 4 *
4 / 10 = 0 done
Get it
Here's the C implementation of the algorithm, which implies my answer. He will find any number in any whole. This is essentially the same as Shakti Singh's answer, except that it works for negative integers and stops as soon as the digit found ...
const int NUMBER = 457;
const int DIGIT_TO_FIND = 5;
int thisNumber = NUMBER >= 0 ? NUMBER : -NUMBER;
int thisDigit;
while (thisNumber != 0)
{
thisDigit = thisNumber % 10;
thisNumber = thisNumber / 10;
if (thisDigit == DIGIT_TO_FIND)
{
printf("%d contains digit %d", NUMBER, DIGIT_TO_FIND);
break;
}
}
source
share