471,049 Members | 1,457 Online

# how can i count the digits of number given by the user

how can i count the digits of number given by the user
Nov 22 '07 #1
8 3856
Meetee
931 Expert Mod 512MB
how can i count the digits of number given by the user
You can take the value in string and can count by counting each char of that string until the end of the string. Search google for more. And if doubt, paste your efforts in the next post.

Regards
Nov 22 '07 #2
You can take the value in string and can count by counting each char of that string until the end of the string. Search google for more. And if doubt, paste your efforts in the next post.

Regards
Above solution is good. But if you some computing you can do as following pseudo code
Expand|Select|Wrap|Line Numbers
1. number = user input
2. numberDigits = 1
3. place = 10
4. while number >= place
5. {
6. numberDigits++
7. place *= 10
8. }
9.
output number
Nov 22 '07 #3
mailavi
13
Above solution is good. But if you some computing you can do as following pseudo code

number = user input
numberDigits = 1
place = 10
while number >= place
{
numberDigits++
place *= 10
}

output number

there is another way....... first u take the number from the user into a variable then divide the number with 10 u would get the unit's place digit the so on to get the individual number and add them........
Nov 29 '07 #4
Ciao Bettina,
please prompt, the user for a string ,not an integer;and then take a look to the ANSI C header <string.h> ,there is the function strlen wich counts the number of characters in a string,that could be useful!
Nov 29 '07 #5
oler1s
671 Expert 512MB
Simpler method:

Take input into a string. Convert that string into a number. Then number of digits is log(number)+1.
Nov 29 '07 #6
Ganon11
3,652 Expert 2GB
Simpler method:

Take input into a string. Convert that string into a number. Then number of digits is log(number)+1.
Just to clarify, this is log with base 10. I'm not sure if the log function in cmath evaluates log base 10, log base 2, or natural log (log base e). To make sure, use log(number)/log(desired base). No matter what base the log function is, dividing by log(desired base) results in the same answer as a log to the correct base of number.
Nov 29 '07 #7
oler1s
671 Expert 512MB
There's a both a log (natural log) and log10 (base 10 log) function in the math library.
Nov 29 '07 #8
according to compilers,there can be different implementation of the logarithm function;but basically log is computed dividing the input iteratively by ten,checking if it is >0 and incrementing a counter;we know that a microprocessor performs division through a series of right shifts;so computing the logarithm consist basically of shifting,comparing and incrementing registers;strlen only perfrom a comparison and increment a counter,without stressing ALU with shifts;though strlen algorithm has linear complexity,so it is not a big advantage to use it when the string is "long"
Nov 29 '07 #9