Write a program in C to count the digits of a given number using recursion.
Input:
Input a non-negtive integer n.
Output:
Print the number of digits in the number n.
Input Sample:
50
Output Sample:
The number of digits in 50 is: 2
这道题是让我们用递归的方法求给定一个数的位数。 注意输出结果有两个( n对应getdata)
#include <stdio.h>
int getdata(int n)
{
if(n<10)
return 1;
else
return 1+getdata(n/10);
}
int main()
{
int n;
scanf("%d",&n);
printf("The number of digits in %d is: %d\n",n,getdata(n));
return 0;
}