Source code:
//Write a program in C to count the number of digits of a given number using recursion.
#include<stdio.h>
int noOfDigits(int n)
{
static int digit=0;
if(n!=0)
{
digit++;
noOfDigits(n/10);
}
return digit;
}
int main()
{
int n,digit;
printf("Enter a number: ");
scanf("%d",&n); /*Enter Number of digits */
digit = noOfDigits(n);
printf("The number of digits in the number is : %d\n",digit);
return 0;
}
Output:
No comments:
Post a Comment