Online Compiler C

#include <stdio.h> int main() { int n; scanf("%d",&n); int counter_1=0; int counter_2=0; while(n>0){ int last=n%10; if(last%2==0){ counter_2++; }else{ counter_1++; } n=n/10; } printf("%d %d",counter_2,counter_2); return 0; }
这段代码的功能是读取一个整数,然后统计这个整数中各位数字里偶数和奇数的个数。但输出部分有一个明显的错误:它打印了两次偶数计数,而没有打印奇数计数。

- 在 `printf` 语句中,你写的是 `counter_2` 两次,应该把其中一个改为 `counter_1`,这样才能正确输出偶数和奇数的个数。
- 注意变量命名:`counter_1` 和 `counter_2` 容易混淆,建议使用更有意义的名字,比如 `even_count` 和 `odd_count`,这样代码更清晰。