c语言 - 1到100里共输出多少个数字9(小白首选,手把手教你读语句)

it2026-02-24  5

1到100里共输出多少个数字9

步骤:

设参数 i ,用for循环遍历1-100。设参数count,数字9每增加一个就+1。for里嵌套两个if语句。 当 i 对10取余为9时,count+1;当 i 整除10为9时,count+1。最后输出count值。

代码:

#include<stdio.h> #pragma warning (disable:4996) #include<windows.h> int main() { int i ; int count = 0; for (i = 1; i <= 100; i++) { if (i % 10 == 9) count++; if (i / 10 == 9) count++; } printf("输出了%d个9。\n", count); system("pause"); }
最新回复(0)