i want this answer rapidly plz

Pages: 12
@blackcoder41:

That does not quite work. consider the case where num == 0 to begin with.
How many digits does the number 0 have? Your solution returns 0; tifa's
returns 1.
haha i know..
I started in the C++ from a very short period of that reality in many errors
LOL i fell asleep in front of the computer last time..

hey i have tweak my code..
1
2
3
4
5
int count(int num, int b=0) {
    if(b==0 && num==0) return 1;
    if(num!=0) return 1 + count(num/10, 1);
    else return 0;
}
Last edited on
1
2
3
4
5
6
7
int num_digits_helper( int num ) {
    return num ? 1 + num_digits_helper( num / 10 ) : 0;
}

int num_digits( int num ) {
    return num ? num_digits_helper( num ) : 1;
}


is better, as long as the helper function is not accessible to the user.

Topic archived. No new replies allowed.
Pages: 12