Program to count the digits of a given number using recursion in C++.
Solution :
#include <iostream>
using namespace std;
int CountD(int n) {
static int count=0;
if (n > 0)
{
count++;
CountD(n/10);
}
return count;
}
int main()
{
int n, ans;
cout<<"Enter a Number :- ";
cin>>n;
ans=CountD(n);
cout<<"Total Number of Digits are :- "<<ans;
return 0;
}
Output :
Enter a Number :- 153 Total Number of Digits are :- 3