C program to count number of digits in a number




Code 1:
Count the number of digits in c programming language

#include<stdio.h>
int main(){
  int num,count=0;

  printf("Enter a number: ");
  scanf("%d",&num);

  while(num){
      num=num/10;
      count++;
  }
  printf("Total digits is:  %d",count);
  return 0;
}

Sample output:
Enter a number: 23
Total digits is:  2

Code 2:
C code to count the total number of digit using for loop

#include<stdio.h>
int main(){
  int num,count=0;

  printf("Enter a number: ");
  scanf("%d",&num);

  for(;num!=0;num=num/10)
      count++;

  printf("Total digits is:  %d",count);

  return 0;
}

Sample output:
Enter a number: 456
Total digits is:  3

Code 3:
Count the digits of a given number in c language using recursion

#include<stdio.h>

int countDigits(num);
int main(){
  int num,count;

  printf("Enter a number: ");
  scanf("%d",&num);

  count = countDigits(num);

  printf("Total digits is:  %d",count);
  return 0;
}

int countDigits(int num){
    static int count=0;

     if(num!=0){
          count++;
         countDigits(num/10);
    }

    return count;
}

Sample output:
Enter a number: 1234567
Total digits is:  7





26 comments:

  1. THANK YOU SOOOOOO MUCH

    ReplyDelete
  2. ITS VERY USEFUL

    ReplyDelete
  3. much thanks for helping people like us to learn c program logic...may ur service for education continue

    ReplyDelete
  4. hehehehe..............:-))
    nyc one
    very useful

    ReplyDelete
  5. Thank you so much.... It helps me alot

    ReplyDelete
  6. hi
    did u tried with the number having digits more then 10,it is giving some junk value

    ReplyDelete
  7. let alone two succinct calls of the recursive one. lame!

    ReplyDelete
  8. thanks alot

    ReplyDelete
  9. print 1-100.....without loop...how to print?

    ReplyDelete
    Replies
    1. #include
      main()
      {
      static int i=1;
      if((printf("%d\n",i++))&& i<=100)
      main();
      }

      Delete
  10. wid the help of recursion..

    ReplyDelete
  11. but this progarm run to count only 10 digit after that what should we do

    ReplyDelete
  12. but this program will not count 0 which is also a digit.
    for example 123=3
    43078=5
    0=1
    we count digits
    0 is also a number
    So Correct ur program.

    ReplyDelete
  13. hi Anand Barnwal you used main(); inside if condition i dint get can u explain it.. thanks

    ReplyDelete
    Replies
    1. here main() is called....in main function.....recursively...untill i>100 :)

      Delete
  14. thankx a lot i think it will help me a lot. thanx again

    ReplyDelete
  15. There is much faster way which has complexity about (log(log10(n)))^2
    For large numbers like 10^1000, you would need 1000 operations for all previous codes, with the code below around 50.
    It is basically a binary search over the exponent of 10 in binary form. For example for 10^27 we search 1-2-4-8-16-32, then 1-2-4-8-16, then 1-2-4, then 1-2, and that is 16 guesses.
    For small numbers you might not see any benefits, but already for 10^32, instead of 32, you have only 8 operations.

    #include
    int main(){
    int n;

    printf("Enter a number: ");

    scanf("%d",&n);

    int r = 10;
    int d = 1;

    int t = 0;
    int p = r;

    int dig=1;

    while(1)
    {
    while (1)
    {

    if (n < t+r) break;

    p = r;
    r = r*r;
    d = 2*d;

    }

    if (d==1) break;

    dig += d/2;
    n = n/p;
    d = 1;
    t = 0;
    p = r = 10;
    }

    printf("Total digits is: %d\n", dig);

    return 0;
    }

    ReplyDelete
  16. The following solution is even faster, although it needs log2(log10(n)) additional space because it uses recursion. It needs only log2(log10(n)) calls. It is basically a wrapped binary decomposition of exponent of 10.

    void nextLevel(int* n, int r, int* dig, int d)
    {

    int t=r;
    r=r*r;

    if(*n>=r)
    {
    d = 2 * d;
    nextLevel(n,r,dig,d);

    if(*n>=r)
    {
    *n=*n/r;
    *dig += d;
    }
    }
    }

    main()
    {
    int n;
    printf("Enter a number: ");

    scanf("%d",&n);

    int r = 10;
    int dig = 1;
    int d = 1;
    nextLevel(&n, r, &dig, d);
    if(n>=10) dig++;

    printf("%d", dig );
    }

    ReplyDelete
    Replies
    1. This line int t=r; is not needed

      Delete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Write down the definition of a function count2s which takes a number as argument and returns the count of the digit 2 in it?

    ReplyDelete