i want this answer rapidly plz

Pages: 12
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
i tried to find a solution but i dont find
You need to write only 1 line of code for that function...
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...
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
thnx jsmith
but how can i can put it in a complete function
???
[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?
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. :-)
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..
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;
}
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.
@kevinkjt2000
sorry but "recursive" is the magic word..
@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.
common tifa.. you can do it, post here if you already solved the problem.. i love final fantasy don't let me down..
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...
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.
#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;
}
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().
@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
Pages: 12