题目:打印3的倍数
要求:编写代码打印0-100之内3的所有倍数
思路:只要可以被3整除的数都是3的倍数
代码实现
解法一:
int main() { int i = 0; int count = 0; for (i = 1; i <= 100; i++) { if (i % 3 == 0) { printf("%d ", i); count++; } } printf("\nconut=%d\n", count); return 0; }
运行结果
解法二
```
int main()
{
int a = 1;
int count = 0;
while (a <= 100)
{
if (a % 3 == 0)
{
printf("%d ", a);
a++;
count++;
}
else
a++;
}
printf("\nconut=%d\n", count);
return 0;
}
运行结果
![](https://s4.51cto.com/images/blog/202010/31/6c3aed517bd16348d905ae33db2590e3.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)