Is there a way to code a recursive function that prints the number of the digits in a number such that:
-It is a void function
-The "if" condition is if(num==0), return
-The "else" will call the recursion.
I saw 2 different types of codes, one of them is where the "if" condition has the recursive call and the else is for "return". But thats not what I want.
I am pretty bad with recursion and trying to understand it by coding myself, but unsuccessfully.
This is my code(I understand why it prints 122 instead of 3 but I dont really know how code it differently. Help anyone?)
#include <iostream>
#include <string.h>
using namespace std;
void numOfDigits(unsigned int num);
int main(){
int num = 994;
numOfDigits(num);
}
void numOfDigits(unsigned int num)
{
int size = 1;
if (num==0)
return;
else
{
if (num / 10 != 0)
size++;
numOfDigits(num / 10);
}
cout << size;
}