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 functionint count_digits(int number) {// Base Caseif (abs(number)/10 == 0) {return 1;}// Recursive Caseelse {return 1 + count_digits(number / 10);}}// main functionint main() {// Initialize numberint number = 8625;// Declare variable resultint result;// Call count_digits function in main and store the returned value in resultresult = count_digits(number);// Print value of resultcout << "Number of digits = " << result;return 0;}
Explanation
count_digits
function
The recursive ...