how can i count the digits of number given by the user
how can i count the digits of number given by the user
Collapse
X
-
Tags: None
-
Originally posted by bettina tophow can i count the digits of number given by the user
Regards -
Originally posted by zodilla58You 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
Code:number = user input numberDigits = 1 place = 10 while number >= place { numberDigits++ place *= 10 }
Comment
-
Originally posted by mohammadazimAbove 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........Comment
-
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!Comment
-
Originally posted by oler1sSimpler method:
Take input into a string. Convert that string into a number. Then number of digits is log(number)+1.Comment
-
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,compar ing and incrementing registers;strle n 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"Comment
Comment