输入任意一个非0整数,编程计算该整数每位上的数为偶数的个数。输入0时,表示结束程序。 输入提示信息:"Input an integer:\n" 输入格式:"%d" 输出提示信息和格式:"The number of even digits is %d\n
时间: 2023-07-07 08:30:52 浏览: 225
下面是C语言的实现代码:
```c
#include <stdio.h>
int main() {
int n;
while (1) {
printf("Input an integer:\n");
scanf("%d", &n);
if (n == 0) {
break;
}
int count = 0;
int digit;
while (n != 0) {
digit = n % 10;
if (digit % 2 == 0) {
count++;
}
n /= 10;
}
printf("The number of even digits is %d\n", count);
}
return 0;
}
```
程序使用了一个无限循环,每次循环先读入一个整数n。如果n为0,则跳出循环,程序结束。否则,程序使用一个循环从低位到高位遍历n的每一位,判断该位上的数字是否为偶数,如果是,则计数器加1。最后输出计数器的值,即为n的每位上的数为偶数的个数。
相关问题
用c语言输入一批整数,计算其中偶数的个数,并输出。(0做结束标志)
好的,我可以帮你写一个用C语言实现的程序,实现计算输入的一批整数中偶数的个数,并输出。以下是代码:
```c
#include <stdio.h>
int main() {
int num, count = 0;
printf("请输入一批整数(以0为结束标志):\n");
while (1) {
scanf("%d", &num);
if (num == 0) {
break;
}
if (num % 2 == 0) {
count++;
}
}
printf("输入的整数中偶数的个数为:%d\n", count);
return 0;
}
```
这个程序的思路是,使用一个循环不断读入整数,如果读入的整数为0,就退出循环。在每次读入整数的时候,判断它是否为偶数,如果是,就将计数器加1。最后输出计数器的值,即为输入的整数中偶数的个数。
c语言编写一个从键盘输入任意个整数,输入-1时结束,分别统计其中奇数和偶数的个数。
```c
#include <stdio.h>
int main() {
int num, even_count = 0, odd_count = 0;
printf("请输入整数(输入-1结束):\n");
while (1) {
scanf("%d", &num);
if (num == -1) {
break;
}
if (num % 2 == 0) {
even_count++;
} else {
odd_count++;
}
}
printf("偶数个数:%d\n", even_count);
printf("奇数个数:%d\n", odd_count);
return 0;
}
```
注:本答案使用了无限循环,通过 `break` 语句跳出循环。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![](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)