Count Number of Digits in a Number in Python Using While Loop

In this blog, we will explore a simple and efficient Python program to count the number of digits present In a number using while loop. By breaking down the code step by step, we will explain the underlying logic and operations involved in this program.

Have you ever wondered how to count the number of digits in a given number using Python? Whether you’re a beginner or an experienced programmer, understanding the logic behind this task can be quite useful. Counting the number of digits in a number can be helpful in various scenarios, such as validating input or performing calculations based on the number’s length. 

So, let’s dive into the fascinating world of programming and explore how to count number of digits in a number in Python using while loop.

Python Program to Count the Number of Digits Present In a Number Using While Loop

# Count the Number of Digits Present In a Number using while loop in python

num = 123456
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))

Code Explanation 

Initialising Variables

We start by initialising the variables. We set the variable num to the given number, for example, num = 123456, and the variable count to 0, which will keep track of the number of digits.

Inserting the While Loop

We enter a while loop with the condition num != 0. This loop will continue executing as long as the value of num is not equal to 0.

Updating the Number and Count

Inside the while loop, we perform two operations:

num //= 10: We use the floor division operator // to divide num by 10 and assign the result back to num. This operation essentially removes the rightmost digit from num.

count += 1: We increment the count variable by 1 for each iteration of the loop. This keeps track of the number of digits we have encountered.

Printing the Result

The while loop continues until num becomes 0, meaning we have removed all the digits from the original number. After exiting the while loop, we print the number of digits by concatenating the string “Number of digits: ” with the value of the count variable using the str() function.

Output:

The output of the code is the number of digits present in the input number. In this example, the output will be:

Number of digits: 6

Alternatively, we can also use the following methods: 

Converting to a List:

You can also convert the number to a list of its digits using list comprehension or the map() function, and then find the length of the list. This method involves converting the number to a list and may require additional memory for storing the list.

Using the len() function (inbuilt method):

By converting the number to a string using the str() function, we can directly apply the len() function to determine the number of characters (digits) in the string. This approach is concise and straightforward.

Here, we used the while loop approach as it is easy to understand and implement, especially for beginners. It requires minimal memory and computational resources since it performs a direct calculation without creating additional data structures. It can be easily modified to perform additional operations within the loop if needed.

Conclusion:

This code demonstrates a simple and efficient way to count the number of digits in a given number using a while loop in Python. By repeatedly dividing the number by 10 and updating the count variable, we can keep track of the number of digits present in the original number. This approach allows us to accurately determine the count without the need for complex calculations or additional libraries. The code provides a practical solution for scenarios where counting the digits in a number is required, and it can be easily understood and implemented by beginners in Python programming.

Frequently Asked Questions

Q: How does the count += 1 operation help in counting the digits?

A: The count += 1 operation increments the value of the count variable by 1 for each iteration of the loop. This allows us to keep track of the number of digits encountered so far.

Q: What will happen if num becomes zero?

A: When num becomes zero, it means that we have removed all the digits from the original number, and there are no more digits to count. This condition causes the while loop to exit.

Q: Why do we need to initialise the count variable to zero?

A: Initializing the count variable to zero ensures that we start counting the digits from the beginning. Without initialization, the count variable would retain its previous value, leading to incorrect results.

Q: What if I insert a negative number?

A: The code provided assumes that the given number is a positive integer. If a negative number is passed, it will count the digits of the absolute value of the number. For example, -123456 would be treated as 123456.

Q: Will this code execute if I enter decimal numbers or fractions?

A: No, the code is designed to count the digits of integer numbers only. Decimal numbers or fractions would require additional handling and modification of the code.

About The Author

Leave a Reply