Pages

Tuesday, 16 June 2015

Count total number of digits using recursion

Count total number of digits using recursion

Coding:

#include<stdio.h>
int count(long n)
{
if(n==0)
return 0;
else
return 1+count(n/10);
}
int main()
{
long b;
printf("Please enter the number:");
scanf("%ld",&b);
if(b>0)
printf("Total digit is=%d",count(b));
else
printf("Total digit is=1");
return 0;
}

Output:
Please enter the number:125
Total digit is=3