...

/

Solution Review: Count the Digits in a Number Using Recursion

Solution Review: Count the Digits in a Number Using Recursion

Let's go over the solution review of the challenge given in the previous lesson.

Solution

Press the RUN button and see the output!

Press + to interact
C++
#include <iostream>
using namespace std;
// Recursive count_digits function
int count_digits(int number) {
// Base Case
if (abs(number)/10 == 0) {
return 1;
}
// Recursive Case
else {
return 1 + count_digits(number / 10);
}
}
// main function
int main() {
// Initialize number
int number = 8625;
// Declare variable result
int result;
// Call count_digits function in main and store the returned value in result
result = count_digits(number);
// Print value of result
cout << "Number of digits = " << result;
return 0;
}

Explanation

count_digits function

The recursive ...