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.

If you have any questions just ask.

About the author

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.

View all posts

20 Comments

  • 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!

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

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

    • 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

  • 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.

  • 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!

    • 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

    • 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

  • 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?

  • 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!!

  • 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.

Leave a Reply

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