Here’s a C program to count the number of digits in an integer with output and proper explanation. The program uses while loop.
# include <stdio.h> # include <conio.h> void main() { int n, count = 0 ; clrscr() ; printf("Enter a number: ") ; scanf("%d", &n) ; while(n > 0) { count++ ; n = n / 10 ; } printf("\nThe number of digits is: %d", count) ; getch() ; }
Output of above program
Enter a number: 52
The number of digits is: 2
Explanation of above program
In this program we are first inputting an integer, counting the total number of digits in that number and finally displaying the output to the user. Let’s see how the above program does this.
There are two variables - n to hold the number and count to count the number of digits in that number (n).
Next we are asking the user to enter a number and storing it in the variable n. After that our while loop calculates the number of digits in n.
Next we are asking the user to enter a number and storing it in the variable n. After that our while loop calculates the number of digits in n.
To understand what is happening inside while loop it’s better to take an example. Suppose, user entered 52. The process of while loop with n = 52 is as follows -
- First n > 0 i.e. 52 > 0, so the program will enter in the while loop.
- In the next step, value of count (initialized to 0 at the start of program) is incremented by 1 with the help of C’s auto incrementing operator (++). So after this step, value of count is 1.
- Now the number (n) is divided by 10 and result is stored again in n i.e. n = n / 10 or n = 52 / 10. Now as you know when you divide two numbers and store the result in an integer variable, only integer part of that result is stored in that integer variable not the fractional part. So when you divide 52 by 10, even when the correct result is 5.2 the new value of n will be 5 i.e. after this step n = 5 and count = 1.
- Now that all statements inside the while loop has been executed once, the looping condition is again checked.
- Again n > 0 i.e. 5 > 0, so the program will again enter the while loop and above steps are followed in the same manner, each time incrementing the value of count by 1 and dividing the number by 10.
- Once the value of n becomes 0 or less than 0, the while loop will exit and rest of the program will continue execution.
Tip: To understand the program you can also take different values of n and try the above process yourself. It will help you to understand the process much clearly and helps in improving your debugging ability.
We can use log10(logarithm of base 10) to count the number of digits of positive numbers (logarithm is not defined for negative numbers).
Digit count of N = log10(N) + 1
is a good way
Howdy Mate,
Grazie! Grazie! Grazie! Your blog is indeed quite interesting around Program to count number of digits in an integer | C Program! I agree with you on lot of points!
A subset of a natural language contains these sentences:
1) Triangle ABC.
2) Line Segment AB.
3) Angle A.
In this set, names of triangles are formed by putting three letters together,
all drawn from the alphabet {A,B,C,D,E}. Line segment names are defined by
putting two letters together from the previous alphabet. And angle names are
(suprise!) given by any letter in that alphabet.
Great effort, I wish I saw it earlier. Would have saved my day :)
Thank you,