Hello everyone, welcome back to programminginpython.com! In this post, I will show you how to find the number of digits in a number. It is pretty simple logic.
https://www.youtube.com/watch?v=bpBBcymXP0U”]
You can watch this video on Youtube here
Find the number of digits in a number – Code Visualization
Task
To find the number of digits in a given number.
Approach
- Read the number whose digits to be counted.
- Use a while loop to get each digit of the number using a modulus
//
operator - Increment after a digit is obtained.
- Continue until the value of the number is 0
- Print the total count(number of digits) of the number.
Program
__author__ = 'Avinash' input_num = int(input('Enter any number: ')) count = 0 while input_num > 0: count += 1 input_num //= 10 print("The number of digits in the given number are:", count)
Output
You can watch this video on Youtube here
Course Suggestion
Want to learn Python with strong fundamentals? If yes, I strongly suggest you take the course below.
Course: Fundamentals of Programming in Python
Find more such Math related programs:
- Python program to find Factorial of a given number
- Python program to find area of a triangle (given all sides)
- Python program to find a number is prime or composite
- Python Program to find the LCM of two numbers
- Find square root of a number using exponential operation
- Find square root of a number using sqrt() function