数出1到100的所有整数中含有9的个数。

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
int count = 0;
for (i = 1; i < 101; i++)
{
if ((i % 10) == 9)       //利用模运算算出个位为9的数
{
printf("%d ", i);
count++;          //用count来统计出现了多少个数字
}
if (i / 10 == 9)    //利用商运算得出十位为9的数字
{
printf("%d ", i);
count++;
}
}
printf("\n共有%d个\n", count);
system("pause");
return 0;


}

在本程序中的关键在于99这个数字含有两个9,所以在模运算和商运算中均要统计一次,所以第二个“if”不能用“else if”来写。而且不能用或操作来运算,因为在或操作中只要前数为真,后面的数就不操作了。

猜你喜欢

转载自blog.csdn.net/higher_and/article/details/79686512
今日推荐