讀入一個整數,統計並輸出該數中2的個數。要求定義並調用函數countdigit(number,digit),它的功能是統計整數number 中數字digit 的個數。例如,countdigit(10090,0) 的返回值是3。
int countdigit (int number,int digit){
int d,n,count;
do{
n=number;
d=n%10;
n=n/10;
if (d==digit) count ++;}while (n!=0);
return count;
}
#include
int main(void)
{
int x,t;
printf("Input an integer: ");
scanf ("%d",&x);
t=countdigit(x,2);
printf("Number of digit 2: %d",t);
}
輸出不出來為什麼呢
int countdigit (int number,int digit){
int d,n,count=0;
n=number;
while (n!=0){
d=n%10;
n=n/10;
if (d==digit) count ++;}
return count;
}
#include<stdio.h>
int main(void)
{
int x,t;
printf("Input an integer: ");
x=123;
t=countdigit(x,2);
printf("Number of digit 2: %d",t);
}