如何手动检查给定号码的数字? 例如, 我有数字 3189。我想检查数字并确定它们是否为偶数。我应该把它变成一个数组检查索引吗?我有点失落了。
如果我将其转换为数组,并且数组的每个元素都是该数字的数字之一,那么这就可以了。但我不知道该怎么做?这样的事可能吗?
到目前为止我已经写了;
int count;
int index;
for (int i = 0; i < count; i++){
index = i;
if (index % 2 == 1){
index /= 3;
}
}
但我发现很难概念化。
以下是从数字中提取数字的方法:
int number = 3189;
int even = 0;
int odd = 0;
while (number != 0) {
int digit = number % 10; // Extract the last digit
if (digit % 2 == 0) // check if number is even
even++;
else
odd++;
number = number / 10; // Remove the last digit
}
number = input()
number_str = str(number)
for digit_char in number_str:
try:
digit = int(digit_char)
if digit % 2 == 0:
print(f"The digit {digit} is even.")
else:
print(f"The digit {digit} is odd.")
except:
print("not a digit") #you can pass it if you want