Guys a little help here please

i want to write a function which take an intiger and return the number of the of digits in it i.e
1
2
3
4
5
int i = 123456
func(i)
{
some code
}

output

the number of the digits are 6
do you know what a logarithm is?
no i don't know
hint:

1
2
3
4
5
int i = 123456;

i /= 10;  // divide by 10

cout << i;  // notice that you just "chopped a digit off" of i 
what if i enter 12584585 now this won't work hmmm
You can use the modulo operator ('%'). For example:

1
2
3
int i = 12345;

i %= 10;


i will be equal to 5
yes i know but what if user enter 845769
now it will return 9
but i want it to return 6
1
2
3
4
5
6
7
8
9
10
int x = 542334;
int Amount = 0;

while(x > 0)
{
	x /= 10;
	Amount++;	
}

cout << Amount << endl;


This should work.
Modulo is not what you want. Modulo by 10 just gives you the last digit.

It's a simple pattern here: Dividing by 10 chops off a digit. So just count how many digits you have to shop off until you get them all.

It'll involve a loop.
Last edited on
give me a example of code please
Topic archived. No new replies allowed.