i want this answer rapidly plz

Pages: 12
Jan 22, 2010 at 9:10pm
@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.
Jan 22, 2010 at 9:14pm
haha i know..
Jan 22, 2010 at 9:37pm
I started in the C++ from a very short period of that reality in many errors
Jan 23, 2010 at 7:19am
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 Jan 23, 2010 at 7:21am
Jan 23, 2010 at 2:59pm
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