打印输出区间内所有谁的倍数

打印0~100内所有的3的倍数

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i++)
{
if (i % 3 == 0)//i/3 是i除以3 i%3是i除以3的余数
printf("%d ", i);
}
system(“pause”);
return 0;
}
在这里插入图片描述
我们可以定义一个count=0;每次打印之后count++,最后打印count的值,这样就能得到一共有多少个3的倍数

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i++)
{
if (i % 3 == 0)//i/3 是i除以3 i%3是i除以3的余数
count++;//每次if执行,3的倍数+1,count的值+1
printf("%d “, i);
}
printf(”\n");//换行美观一点
printf(“3的倍数一共有:%d\n”,count);//打印最终结果
system(“pause”);
return 0;
}
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_54748281/article/details/113525791
今日推荐