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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bettina top
    New Member
    • Nov 2007
    • 1

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

    how can i count the digits of number given by the user
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by bettina top
    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

    Comment

    • mohammadazim
      New Member
      • Aug 2007
      • 28

      #3
      Originally posted by zodilla58
      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
      Code:
      number = user input
      numberDigits = 1
      place = 10
      while number >= place
      {
      numberDigits++
      place *= 10
      }
      output number
      Last edited by pbmods; Nov 30 '07, 12:06 AM. Reason: Added CODE tags.

      Comment

      • mailavi
        New Member
        • Aug 2007
        • 13

        #4
        Originally posted by mohammadazim
        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........

        Comment

        • MarshMallow
          New Member
          • Nov 2007
          • 52

          #5
          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

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            Simpler method:

            Take input into a string. Convert that string into a number. Then number of digits is log(number)+1.

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Originally posted by oler1s
              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.

              Comment

              • oler1s
                Recognized Expert Contributor
                • Aug 2007
                • 671

                #8
                There's a both a log (natural log) and log10 (base 10 log) function in the math library.

                Comment

                • MarshMallow
                  New Member
                  • Nov 2007
                  • 52

                  #9
                  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

                  Working...