通過程式設計實現,統計1~n有多少個9 提示:n通過引數傳入
阿新 • • 發佈:2019-01-26
#include <stdio.h>
int fun(int n)
{
int i;
int tmp;
int k;
int count = 0;
for(i=1;i<=n;i++)
{
tmp = i;
while(tmp != 0)
{
if(tmp == 9 || k == 9)
{
count++;
}
k = tmp % 10;
tmp = tmp / 10 ;
}
}
return (count-1);
}
int main()
{
int num;
int count = 0;
printf("Please input n:");
scanf("%d",&num);
count = fun(num);
printf("the number of 9 between 1 and %d is:%d\n",num,count);
return 0;
}