C语言,输入一个正整数,统计并输出数中2的个数#includeint main(void){ int countdigit(int number,int digit); int countdigit,digit=2,number; scanf("%d",number); countdigit=countdigit(number,digit); printf("Number of digit 2:%d",countdigi
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/06 00:41:51
C语言,输入一个正整数,统计并输出数中2的个数#includeint main(void){ int countdigit(int number,int digit); int countdigit,digit=2,number; scanf("%d",number); countdigit=countdigit(number,digit); printf("Number of digit 2:%d",countdigi
C语言,输入一个正整数,统计并输出数中2的个数
#include
int main(void)
{
int countdigit(int number,int digit);
int countdigit,digit=2,number;
scanf("%d",number);
countdigit=countdigit(number,digit);
printf("Number of digit 2:%d",countdigit);
return 0;
}
int countdigit(int number,int digit)
{
int a,count=0;
while(number!=0){
a=number%10;
number=number/10;
if(a==2)
count++;}
return count;
}
C语言,输入一个正整数,统计并输出数中2的个数#includeint main(void){ int countdigit(int number,int digit); int countdigit,digit=2,number; scanf("%d",number); countdigit=countdigit(number,digit); printf("Number of digit 2:%d",countdigi
修改如下:
1.主函数中int countdigit, digit = 2, number 中,countdigit重复定义,改为其他变量, 比如x
2.改为 scanf("%d", &number);
3.printf("Number of digit 2:%d", x); // 上述修改过后的变量x
完整修改如下:
#include
int main(void)
{
int countdigit(int number,int digit);
int x,digit=2,number;
scanf("%d",&number);
x=countdigit(number,digit);
printf("Number of digit 2:%d\n",x);
return 0;
}
int countdigit(int number,int digit)
{
int a,count=0;
while(number!=0){
a=number%10;
number=number/10;
if(a==2)
count++;
}
return count;
}