i want this answer rapidly plz

Pages: 12
Jan 22, 2010 at 1:52pm
recursive function retrns number of digits in any integer
9325------->4
i want acomplete code plz
for example
9325 should return 4
Last edited on Jan 22, 2010 at 2:15pm
Jan 22, 2010 at 2:03pm
Jan 22, 2010 at 2:16pm
i tried to find a solution but i dont find
Jan 22, 2010 at 2:21pm
You need to write only 1 line of code for that function...
Jan 22, 2010 at 2:53pm
Note that for an integer x, x % 10 gives you the ones digit and x / 10 gives you everything but the ones digit.
ie, 1234 % 10 == 4, 1234 / 10 = 123. Perhaps you could repeatedly divide by 10 until... and count something...
Jan 22, 2010 at 3:10pm
string to int http://cplusplus.com/articles/numb_to_text/
then get the length of string..

edit:
argh.. sorry i didn't notice the word recursive..
+1 jsmith
Last edited on Jan 22, 2010 at 4:23pm
Jan 22, 2010 at 3:52pm
thnx jsmith
but how can i can put it in a complete function
???
Jan 22, 2010 at 4:39pm
[sarcasm]
Use a text editor, and make sure that your function has both an opening brace and a closing brace.
[/sarcasm]

Come on, think about it a little bit. Didn't anything I say help?
Jan 22, 2010 at 4:48pm
FYI, nobody's going to write your code for you. jsmith gave some very good information.

Think about it some, attempt a function on your own, post it here for us to look at, and we'll be glad to help you tweak it some to get it right...but you have to try first. :-)
Jan 22, 2010 at 5:11pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main () {
    int number, count = 0;
    cin >> number;

    while(true) {
        if(number!=0) count++;
        else break;
        number/=10;
    }

    cout << count;
	return 0;
}


this is just a hint.. convert it to recursive.. maybe now you can see a picture of it..
Jan 22, 2010 at 5:17pm
i wrote this code but it doesn't run

#include<iostream.h>
int digit(int);
void main()
{
int x=584;
cout<<digit(x);
}
int digit(int n)
{
int count =0;
digit(n/10);
count++;
return count+1;
}
Jan 22, 2010 at 6:25pm
Just include <cmath> and then use the log function and add 1 and truncate.
For example:
log(1234)~=3.0913151596972228772592050619999
add 1: 4.0913151596972228772592050619999
truncate: 4

So long as you do not have numbers greater than zero and they are integers this trick will work. Including decimal places can get a little bit trickier.
Jan 22, 2010 at 6:39pm
@kevinkjt2000
sorry but "recursive" is the magic word..
Jan 22, 2010 at 6:44pm
@kevin,

HAHA, that's priceless. Especially since tifa is obviously using this site to do his homework for him. Too bad that solution doesn't utilize recursion. His teacher would freak out.
Jan 22, 2010 at 6:47pm
common tifa.. you can do it, post here if you already solved the problem.. i love final fantasy don't let me down..
Jan 22, 2010 at 7:19pm
Every invocation of digit() gets its own local variable count, so every invocation returns 2 (you increment
count [to 1], then return the value of count plus 1.

When does digit() stop recursing?

You call digit with 584. That digit() then calls digit() with 584/10 = 58. THAT digit() then calls digit() with 58/10 = 5.
THAT digit() then calls digit() with 5/10 = 0. THAT digit() then calls digit() with 0/10 = 0. THAT digit() then calls
digit() with 0/10 = 0. ... etc...
Jan 22, 2010 at 7:38pm
Ooops... didn't notice "recursive" so I guess if he could recursively define log I guess.... but thats just too much work, oh well. Maybe he should ask the teacher for help if he is doing his homework.
Jan 22, 2010 at 8:27pm
#include<iostream>
#include<conio.h>
using namespace std;

int digit(int);//function proto type.
int main()
{
int num;
cout<<"Enter any number";
cin>>num;
cout<<"The number contain "<<digit(num)<<"digits"<<endl;
cin>>num;
cout<<digit(num)<<endl;
cin>>num;
cout<<digit(num)<<endl;

getch();
return 0;
}
int digit(int n)
{
static int count=0;
if((n/10)==0)
count=0;
else
{

digit(n/10);
count++;
}
return count+1;
}
Jan 22, 2010 at 8:35pm
Well, that appears to work, albeit in a convoluted way.

You should try to get rid of the static count variable in favor of using the return value from
the recursive calls to digit().
Jan 22, 2010 at 8:42pm
@tifa farouk
nice job! i know you can do it.. but please fix your post..
read this http://cplusplus.com/articles/firedraco1/

here's my solution.. use it for learning.

1
2
3
4
5
6
int count(int num) {
    if(num!=0) {
        num/=10;
        return 1 + count(num);
    } else return 0;
}
Last edited on Jan 22, 2010 at 9:03pm
Pages: 12