c++实现给定一个幸运数,如果他的每个数字是6或者8,那么这个数字就是幸运数
时间: 2024-05-24 07:10:30 浏览: 15
#include <stdio.h>
int isLucky(int n) {
while (n > 0) {
int digit = n % 10;
if (digit != 6 && digit != 8) {
return 0;
}
n /= 10;
}
return 1;
}
int main() {
int n;
printf("请输入一个数字:");
scanf("%d", &n);
if (isLucky(n)) {
printf("%d是幸运数字\n", n);
} else {
printf("%d不是幸运数字\n", n);
}
return 0;
}
相关问题
c++实现给定一个幸运数,如果他的每个数字是6或者8,那么这个数字就是幸运数字
#include <stdio.h>
int isLucky(int number);
int main() {
int number;
printf("请输入一个数字:");
scanf("%d", &number);
if (isLucky(number)) {
printf("%d是幸运数字\n", number);
} else {
printf("%d不是幸运数字\n", number);
}
return 0;
}
int isLucky(int number) {
while (number > 0) {
int digit = number % 10;
if (digit != 6 && digit != 8) {
return 0;
}
number /= 10;
}
return 1;
}
c++给定一个幸运数,如果他的每个数字是6或者8,那么这个数字就是幸运数字
。以下是一个判断一个数字是否为幸运数的Python程序示例:
```python
def is_lucky_number(num):
for digit in str(num):
if digit not in ['6', '8']:
return False
return True
# 示例
print(is_lucky_number(6868)) # True
print(is_lucky_number(1234)) # False
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)