1. 程式人生 > >.通過程式設計實現,統計1~n有多少個9 (n通過引數傳入)

.通過程式設計實現,統計1~n有多少個9 (n通過引數傳入)



#include<stdio.h>

int count9(int n)
{
        int i,r,temp;
        int j=0;
        for(i=1;i<=n;i++)
        {
                temp=i;
                while(temp!=0)
                {
                        r=temp%10;
                        temp=temp/10;
                        if(r==9)
                        {
                                j++;
                        }
                }
        }
        return j;
}
int main()
{
        int n,m;
        printf("input a number:\n");
        scanf("%d",&n);
        m=count9(n);
        printf("The number of 9 is:%d\n",m);
        return 0;
}