C语言查找一个整数出现的次数

C语言查找一个整数出现的次数

思路分析:
首先循环输出1-100的所有整数,用整数%10,等于9,即求出了所有各位是9的个数,用整数除10,得到了所有十位是9的个数,有一个数字比较特殊,99我们应该算两次,所以,应该用两个判断语句进行判断。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main() {
	int i = 0;
	int count = 0;//定义一个记录次数的变量
	for (i = 1; i <= 100; i++) {
		if (i % 10 == 9) {
			count++;//99进来一次count++
		}
		if (i / 10 == 9) {
			++count;//99进来count++
		}
	}
	printf("1-100中出现9的次数有%d次\n", count);
	system("pause");
	return 0;
}

输出结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/a_hang_szz/article/details/88681004