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





34 comments:

Anonymous said...

THANK YOU SOOOOOO MUCH

Anonymous said...

AMAZING

Anonymous said...

great

Anonymous said...

ITS VERY USEFUL

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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

sriram's for you said...

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

Anonymous said...

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

Murali Nimmala said...

Thanks for your time.

adamu musa dutse said...

thousand of thanks admin

Anonymous said...

thanks alot

Anonymous said...

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

Anand Barnwal said...

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

Anonymous said...

wid the help of recursion..

Ajay Jain said...

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

Awadhesh Kumar said...

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.

Sujendra B said...

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

Anonymous said...

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

Zakkeel Mohammed Nisardeen said...

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

Alex Peter said...

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;
}

Alex Peter said...

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 );
}

Alex Peter said...
This comment has been removed by the author.
Alex Peter said...

This line int t=r; is not needed

Anonymous said...

thanks

Siraj Munir said...

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

Fuser said...

Your program to calculate the no of digits in an integer is incorrect.
As an example try 20000.

suhas said...

need count the digits of a given number using do while & while loop

Suba Gj said...

*
* *
* * *
* *
*
i want this program.plz help me

Mostofa Asif said...

1. int calculateOccurrenceOfDigit(int number, int digit);

Complete the above prototyped function that will return total number of occurrence of digit in a number. For e.g. calculateOccurrenceOfDigit( 102015400, 0) will return 4 as it has 4 zeros.
You have to use recursion.

int calculateOccurrenceOfDigit(int number, int digit)
{
.............. i need the answer

Unknown said...

but it won t work if number have more than 10 digits

Nishant Ranjan said...

Nicely Done.!

putta vijitha said...

<it's really super>

aravind mishra said...

In this program have one mistake..that is (int num) 0 cannot be read the compiler,so using (long int num) the program will correctly executed in more than digits..