您的位置:首页 > 其它

统计1~n有多少个9 ,n通过参数传入

2016-10-28 23:22 155 查看
//通过编程实现,统计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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: