问题描述:
编写一个函数,功能:从一个整数m中,统计其中各位上等于n的数字数目,并返回,其中0<=n<=9,若n越界,则返回-1,并提示‘第二个参数越界’。例如4500201中
有0共3个,编写主函数并调试。
代码展示:
1 #include<stdio.h>
2 int fun(long num, int n);
3 int main(){
4 long num;
5 int n;
6 int result;
7 printf("请输入一长串整数:");
8 scanf("%d", &num);
9 printf("请输入统计的数字n:");
10 scanf("%d", &n);
11 result = fun(num, n);
12 printf("%d的个数有:%d\n",n,result);
13 return 0;
14 }
15 int fun(long num, int n){
16 int count = 0;
17 int temp;
18 while(num!=0){
19 temp = num%10;
20 num /= 10;
21 if(temp == n){
22 count++;
23 }
24 }
25 return count;
26 }
运行截图: