
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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,
Input-1 −
N = 891452
Output −
6
Explanation − Since the given number 891452 contains 6 digits, we will return ‘6’ in this case.
Input-2 −
N = 0074515
Output −
5
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.
Example
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))
Output
Running the above code will generate the output as,
5
- Related Articles
- Java program to Count the number of digits in a given integer
- Program to count number of stepping numbers of n digits in python
- Golang Program to Count the Number of Digits in a Number
- Write a program in Python to count the total number of leap years in a given DataFrame
- Count digits in given number N which divide N in C++
- Program to count number of elements in a list that contains odd number of digits in Python
- Write a Golang program to find the sum of digits for a given number
- Write a python program to count total bits in a number?
- How to find the number of digits in a given number using Python?
- Write a program to reverse digits of a number in C++
- Program to count number of islands in a given matrix in Python
- C++ Program to Sum the digits of a given number
- How to count digits of given number? JavaScript
- Write a program in Python to count the total number of integer, float and object data types in a given series
- Python program to count the number of vowels using set in a given string

Advertisements