//通过编程实现,统计1~n有多少个9 ,n通过参数传入
#include <stdio.h>
int num(int a);
int main()
{
int n; //n为输入的整数变量
printf("Please input a num:\n");
scanf("%d",&n);
num(n);//调用num函数
return 0;
}
int num(int a)
{
int i;
int count = 0;//count用来统计有多少个9,
int b = 0;
int c = 0;
for(i = 1;i <= a;i++)
{
c = i;
while(c)
{
b = c % 10;//取个位
c = c / 10;//去掉一位,继续判断
if(b == 9)
{
count++;//count用来计数
}
}
}
printf("9的个数为:%d\n",count);
return 0;
}
统计1~n有多少个9 ,n通过参数传入
最新推荐文章于 2020-01-24 21:51:50 发布