457 % 10 = 7 *
457/10 = 45
45 % 10 = 5 *
45/10 = 4
4 % 10 = 4 *
4/10 = 0 done
¿Enterece?
Aquí hay una implementación C del algoritmo que implica mi respuesta. Encontrará cualquier dígito en cualquier número entero. Es esencialmente la misma exacta como la respuesta de Shakti Singh excepto que funciona para los enteros negativos y se detiene tan pronto como se encuentre el dígito ...
const int NUMBER = 457; // This can be any integer
const int DIGIT_TO_FIND = 5; // This can be any digit
int thisNumber = NUMBER >= 0 ? NUMBER : -NUMBER; // ?: => Conditional Operator
int thisDigit;
while (thisNumber != 0)
{
thisDigit = thisNumber % 10; // Always equal to the last digit of thisNumber
thisNumber = thisNumber/10; // Always equal to thisNumber with the last digit
// chopped off, or 0 if thisNumber is less than 10
if (thisDigit == DIGIT_TO_FIND)
{
printf("%d contains digit %d", NUMBER, DIGIT_TO_FIND);
break;
}
}
El valor '' 457' int' en realidad no "contener" el número '5'. Su representación decimal sí lo hace. –