- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in Python to count the number of digits in a given number N
Let's suppose we have given a number N. the task is to find the total number of digits present in the number. For example,
N = 891452
Explanation − Since the given number 891452 contains 6 digits, we will return ‘6’ in this case.
N = 0074515
Explanation − Since the given number 0074515 contains 5 digits, we will print the output as 5.
The approach used to solve this problem
We can solve this problem in the following way,
Take input ‘n’ as the number.
A function countDigits(n) takes input ‘n’ and returns the count of the digit as output.
Iterate over all the digits of the number and increment the counter variable.
Return the counter.
def countDigits(n): ans = 0 while (n): ans = ans + 1 n = n // 10 return ans n = “45758” print("Number of digits in the given number :", countDigits(n))
Running the above code will generate the output as,
- Java program to Count the number of digits in a given integer
- Golang Program to Count the Number of Digits in a Number
- Program to count number of stepping numbers of n digits in python
- Write a Golang program to find the sum of digits for a given number
- Count digits in given number N which divide N in C++
- Write a program in Python to count the total number of leap years in a given DataFrame
- Write a program to reverse digits of a number in C++
- How to find the number of digits in a given number using Python?
- Write a python program to count total bits in a number?
- C++ Program to Sum the digits of a given number
- Program to count number of elements in a list that contains odd number of digits in Python
- Program to count number of islands in a given matrix in Python
- Find count of digits in a number that divide the number in C++
- Program to find the sum of all digits of given number in Python