C Language: How to count digits of a number?

Often when you are writing a script, you need to know how many digits will your input have. Function that  I’ll share with you counts digits of an integer (“100” – 3 digits; “12”- 2 digits).  User inputs number, scripts counts number of digits of input.

 /*function prototypes */
 int count_numbers (int num);
/* main function ask for integer, call the
count_number function and show the result */
int main (void) {
  int number=0, x=0;

  printf("Please enter an integer: ");
  scanf("%d", &number);
  x=count_numbers (number);
  printf("n%d consist of %d digits!", number, x);

  return 0;
}
/* Getting input from main function, counting until
input that we get is not equal to zero,
incrementing counter by one each time */
int count_numbers ( int num) {

  int count =0;
  if (num == 0) count++;
  while (num !=0) {
    count++;
    num/=10;
  }
  return count;

}

If you have any questions just ask.

Thanks for installing the Bottom of every post plugin by Corey Salzano. Contact me if you need custom WordPress plugins or website design.

Anatoly Spektor

IT Consultant with 6 years experience in Software Development and IT Leadership. Participated in such projects as Eclipse IDE, Big Blue Button, Toronto 2015 Panam Games.

Join the Discussion

Your email address will not be published. Required fields are marked *

Discussion

  1. hasan

    Thank you this post… It helped me! Looking forward for your other C functions….

  2. Andy_G

    Yo, i got task dunno what to do with it. If you have time you could help. Here it is:

    ” Write a function called check_passwd() that will compare two strings. It takes two strings as parameters, and returns a character. If the two strings are equal then “A” returned, otherwise the first character that is different from the first string will be returned. For example: ”

    10x man!

  3. shadab

    but it only works for not more than 5 digits………..

    1. Anatoly Spektor

      Why 5 ? You can enter as long number as integer can hold, this program does not restrict number to any particular length.

  4. shadab

    #include
    int main()
    {
    long int number,dgtcnt=0;
    printf(“Enter an integer number: “);
    scanf(“%ld”,&number);

    while(number){
    number=numbe/10;
    dgtcnt++;
    }
    printf(“Total no. of digits is: %ld”,dgtcnt);
    getch();
    }

    try this ………………from shadab

  5. shadab

    #include
    int main()
    {
    long int number,dgtcnt=0;
    printf(“Enter an integer number: “);
    scanf(“%ld”,&number);

    while(number){
    number=number/10;
    dgtcnt++;
    }
    printf(“Total no. of digits is: %ld”,dgtcnt);
    getch();
    }

    try this ………………from shadab

  6. saurav kumar

    what if number start with zero??

    1. Anatoly Spektor

      Good question, thanks for asking!

      This is a simple program that is intended for natural numbers only. In case of numbers that have zeros in front of it, I would suggest to convert “int” to “string” and count characters.

      Regards,

      Anatoly

  7. Preston

    Hello just wanted to give you a quick heads up and let you know a few
    of the images aren’t loading properly. I’m not sure
    why but I think its a linking issue. I’ve tried it in two different browsers and both show the same results.

    1. Anatoly Spektor

      Hello Preston,

      Could you please be more concrete, and point me to the images that are not loading, so I could fix them.

      Thanks,

      Anatoly

  8. dolphin.silktide.net

    My programmer is trying to convince me to move to .
    net from PHP. I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using Movable-type on various websites
    for about a year and am worried about switching to another platform.
    I have heard excellent things about blogengine.

    net. Is there a way I can transfer all my wordpress posts into it?
    Any kind of help would be greatly appreciated!

    1. Anatoly Spektor

      Hi,

      Thank for your question. First of all I would like to say that every programmer,developer and blogger have their own preferences of what platform to use. Keeping this in mind, I can only give you advice based on my experience.

      I am not a big fan of .NET for 2 reasons:

      1. I totally agree with you when you say that .NET is expensive.

      .NET is not cross platform, in other words it is Microsoft dependant, so if you have OSX or Linux server, or development platform, you will need to buy Windows, Visual Studio etc, which will result in huge expenses which can be EASILY AVOIDED.

      2. WordPress is considered one of the best platforms in the market. It has HUGE community and a lot of plugins, I am pretty sure that there are much more flexibility in wordpress than in blogengine.net.

      Note that programmers like to advice frameworks that they are more familiar with. This is reasonable from their standpoint, but from business standpoint I would advice to use open-source solutions such as wordpress, as they have the same capabilities, but less expensive.

      Good luck,

      Anatoly

  9. Fascinating blog! Is your theme custom made or did you download it from
    somewhere? A design like yours with a few simple tweeks would really
    make my blog stand out. Please let me know where
    you got your theme. Many thanks

    1. Anatoly Spektor

      Hello,

      I am glad that you like the theme I use.

      I use standard wordpress.com theme called Bueno (just changed header a bit and added couple of widgets).

      Good luck to your blog, and I hope to see your here more often!

      Cheers,

      Anatoly

  10. RushKP

    Hey mate, I typed in the program down below to count the digits in a number, as you have done up above, only I used recursion.

    #include
    count(int);
    void main()
    {
    int num;
    int counter;
    scanf(“%d”,&num);
    counter=count(num);
    printf(“%d”,counter);
    }

    count(int a)
    {
    if(a%10==0 && a/10==0)
    return(0);
    else
    return(1+count(a/10));
    }

    My question is what shadab above mentioned- this program returns 5 for any number of >=5 digits…
    Meaning “2343214312431” will show “5” as the number of digits.
    Ideas on why this happens?

  11. RushKP

    Actually its if(a/10==0) up there in the recursion function. There’s no need for the % operation.

  12. Wow! At last I got a blog from where I can genuinely obtain useful information
    concerning my study and knowledge.

  13. sit6534.evenweb.com

    Oh my goodness! Awesome article dude! Many thanks,
    However I am going through difficulties with your RSS.

    I don’t understand the reason why I am unable to subscribe to it.
    Is there anybody having the same RSS problems?

    Anybody who knows the solution can you kindly respond?

    Thanx!!

  14. Rolando

    Enjoy the Newcastle United vs Aston Villa live scoring summaries and highlights
    here in Newcastle United vs Aston Villa live tv sports The FA Cup with Budweiser football match.
    Certainly no team in the league has the talent to sniff the core group that now will be playing their
    home games in Miami. EPL-English Premier League Football Watch Aston Villa vs Newcastle United Live EPL-English Premier League soccer (Online TV,
    Score & Preview 2015) Hey, football lover we are happy to coming EPL-English Premier League football 2015.

arrow