

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the number of digits in a given number using Python?
In this program, we have to find the number of digits in an integer given by the user.
For example
User Input: 123, Output: 3
User Input: 1987, Output: 4
Algorithm
Step 1: Take Integer value as input value from the user
Step 2: Divide the number by 10 and convert the quotient into Integer type
Step 3: If quotient is not 0, update count of digit by 1
Step 4: If quotient is 0, stop the count
Step 5: STOP
Example Code
x = int(input("User Input: ")) count_of_digits = 0 while x > 0: x = int(x/10) count_of_digits += 1 print("Number of Digits: ",count_of_digits)
Output
User Input: 123 Number of Digits: 3 User Input: 1987 Number of Digits: 4
Explanation
When we divide a number by 10 and convert the result into type int, the unit's place digit gets deleted. So, by dividing the result each time by 10 will give us the number of digits in the integer. Once the result becomes 0, the program will exit the loop and we will get the number of digits in the integer.
- Related Questions & Answers
- Find the Largest number with given number of digits and sum of digits in C++
- Program to find the sum of all digits of given number in Python
- Find smallest number with given number of digits and sum of digits in C++
- Find maximum number that can be formed using digits of a given number in C++
- Write a program in Python to count the number of digits in a given number N
- Number of digits in the nth number made of given four digits in C++
- How to find the sum of digits of a number using recursion in C#?
- How to count digits of given number? JavaScript
- C++ Program to Sum the digits of a given number
- Write a Golang program to find the sum of digits for a given number
- Remove repeated digits in a given number using C++
- How to Find the Factorial of a Number using Python?
- C# program to find the sum of digits of a number using Recursion
- Program to find number of bit 1 in the given number in Python
- Python Program to Find the Sum of Digits in a Number without Recursion
Advertisements