## INDEX

### 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 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... Anonymous said...

thanks alot Anonymous said...

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

Anand said...

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

wid the help of recursion..

Unknown said...

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

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

Unknown 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 :)

Unknown said...

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

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

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

Aleksandar said...
This comment has been removed by the author.
Aleksandar said...

This line int t=r; is not needed Anonymous said...

thanks

Unknown 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

Unknown said...

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

Unknown 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)
{

robert said...

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

Nishant Ranjan said...

Nicely Done.!

Unknown said...

<it's really super>

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

Parthiban B said...
This comment has been removed by the author.
Unknown said...

If i enter "0000" as input it shows '0' as output.......
But actually the length of the integer is '4'. Can you please post the solution for it. Thanks in advance.

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

Code 1 why u used num=num/10?

Shankavi said...

This program is correct guys...it also works with 0s...eg:my num is 20000 it prints 5 digits in the number.