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.

About these ads

About Anatoly Spektor

My name is Anatoly Spektor (originally Anatolijs Spektors) I am Software and Web Developer. I have worked in Seneca Center for Development of Open Technology on Big Blue Button Add-on - Polling Module. Currently, I am employed as Software Engineer in Red Hat.
This entry was posted in C, Useful Functions and tagged , , , , , . Bookmark the permalink.

20 Responses to C Language: How to count digits of a number?

  1. hasan says:

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

  2. Andy_G says:

    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 says:

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

  4. shadab says:

    #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 says:

    #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 says:

    what if number start with zero??

    • 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. Whitney says:

    I think this is one of the most vital information for me.
    And i’m glad reading your article. But want to remark on few general things, The website style is wonderful, the articles is really nice : D. Good job, cheers

  8. Preston says:

    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.

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

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

    • 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

  11. RushKP says:

    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?

  12. RushKP says:

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

  13. Just want to say your article is as amazing. The clearness to your submit is simply
    nice and i can assume you are an expert in this subject.
    Well together with your permission allow me to snatch your
    RSS feed to keep updated with forthcoming post. Thank you one million and please carry
    on the enjoyable work.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s