Example 1: Count Number of Digits in an Integer using while loop
num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
Output
Number of digits: 4
In this program, the while loop is iterated until the test expression num != 0
is evaluated to 0 [false].
- After the first iteration,
num
will be divided by 10 and its value will be 345. Then, thecount
is incremented to 1. - After the second iteration, the value of
num
will be 34 and thecount
is incremented to 2. - After the third
iteration, the value of
num
will be 3 and thecount
is incremented to 3. - After the fourth iteration, the value of
num
will be 0 and thecount
is incremented to 4. - Then the test expression is evaluated to false and the loop terminates.
Example 2: Using inbuilt methods
num = 123456
print[len[str[num]]]
Output
6
In the above example, we first convert the integer value into string by using str[]
. Then, we find the length of the string using
len[]
.
This is a Python Program to count the number of digits in a number. Problem Description The program takes the number and prints the number of digits in the number. Problem Solution 1. Take the value of the integer and store in a variable. Program/Source Code
2. Using a while loop, get each digit of the number and increment the count each time a digit is
obtained.
3. Print the number of digits in the given integer.
4. Exit.
Here is source code of the Python Program to count the number of digits in a number. The program output is also shown below.
n=int[input["Enter number:"]] count=0 while[n>0]: count=count+1 n=n//10 print["The number of digits in the number are:",count]
Program Explanation
1. User must first enter the value of the integer and store it in a variable.
2. The while loop is used and the last digit of the number is obtained by using the modulus operator.
3. Each time a digit is
obtained, the count value is incremented.
4. This loop terminates when the value of the number is 0.
5. The total count of the number of digits is printed.
Runtime Test Cases
Case 1: Enter number:123 The number of digits in the number are: 3 Case 2: Enter number:1892 The number of digits in the number are: 4
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Next Steps:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.
Count the number of digits in a number using python :
Using python, count the number of digits in a number. In this tutorial, we will learn how to count the total number of digits in a number using python. The program will get the input from the user and print out the result.
We will show you two different ways to calculate total digits in a number.
Solution 1 [using a while loop] :
The idea behind this solution is to keep deleting the rightmost digit of the number one by one until the number becomes zero.
Following algorithm we are going to use for this approach :
- Take the user input and store it in a variable.
- Create one counter variable to hold the total number count. Initialize this variable to zero at the start of the program.
- Using a while loop, delete the rightmost digit of the number or convert the number to this new number. For example, if the number is 123, convert it to 12, then convert it to 1 and finally 0.
- Increment the counter variable by 1 on each conversion. Do it until the number becomes zero. At the end of the while loop, this counter variable will hold the total digit count of the number.
- Print the counter variable.
Python program :
#example 1
count = 0
number = int[input["Enter a number "]]
while [number > 0]:
number = number//10
count = count + 1
print ["Total number of digits : ",count]
#example 2
count = 0
number = int[input["Enter a number "]]
print ["Total number of digits : ",len[str[abs[number]]]]
You can also download this program from here.
Explanation :
- The code is similar to the algorithm we have explained above. ‘count’ is the counter variable used to hold the total number of digits in the number. This variable is initialized to zero at the beginning.
- We are using the input[] method to read the user input. This method returns a string, we are wrapping this value with int[] to get the integer value of the number user has entered. This value is stored in the ‘number’ variable.
- The ‘while loop’ will run until the value of ‘number’ is greater than zero.
- Inside the loop, we are dividing the number by 10 and assigning this new value to the number. If the value is 123, it will be 12 after the division. It is same as like deleting the last digit of the number.
- Increment the value of count by 1 after the conversion. Eventually, the ‘number’ variable will become zero and the loop will stop.
- Print the value of the ’count’ at the end of the program.
Example :
Solution 2 [ Using inbuilt methods ] :
Instead of using a while loop, we can directly convert the integer to a string and get the length of the string. In python, getting the length of a string is easy by using the ’len[]’ method. For converting a number to a string, we can use the ’str[]’ method.
‘abs’ is used to get the absolute value of a number. i.e. to handle the case even if the number is negative.
For example, if our input value is ’-123‘, abs[] will convert it to ’123‘. str[] will convert it to a string and len[] will return the length of the string or 3.
Python program :
#example 1
count = 0
number = int[input["Enter a number "]]
while [number > 0]:
number = number//10
count = count + 1
print["Total number of digits : ", count]
#example 2
count = 0
number = int[input["Enter a number "]]
print["Total number of digits : ", len[str[abs[number]]]]
You can also download this program from here.
Example :
Similar tutorials :
- Find the number of CPU count using python
- Count the number of words in a file using python
- Count the total number of characters in a string using python
- Count the number of blank spaces in a file using python
- Python program to find LCM of two numbers
- Python program to sort words of a string in alphabetical order