2. Write a recursive method that returns the number of digits that are in a positive integer.
In the given question we have to count the number of digits in a positive integer by using recursion.
Here is the C++ program for doing this-
#include<iostream>
using namespace std;
//Recursive function for counting number of digits
int Digits(long long n){
if (n==0)
return 0;
else
return 1+Digits(n / 10);
}
//Main code
int main(){
long long n;
cout<<" Enter the number "<<endl;
cin>>n;
cout<<" Number of digits in given number are "<<Digits(n);
return 0;
}
For example -
Input- 164528
Output- Number of digits are 6
Get Answers For Free
Most questions answered within 1 hours.