Header Ads

Python Program to Count the Number of Digits in a Number

Python Program to Count the Number of Digits in a Number
Python Program to Count the Number of Digits in a Number
This is a Python Program to count the number of digits in a number.

Problem Description
The program takes the number and prints the number of digits in the number.
Problem Solution
1.         Take the value of the integer and store in a variable.
2.         Using a while loop, get each digit of the number and increment the count each time a digit is obtained.
3.         Print the number of digits in the given integer.
4.         Exit.
Program/Source Code
Here is source code of the Python Program to count the number of digits in a number. The program output is also shown below.



n=int(input("Enter number:"))
count=0
while(n>0):
    count=count+1
    n=n//10
print("The number of digits in the number are:",count)
Runtime Test Cases

Case 1:
Enter number:123
The number of digits in the number are: 3
Case 2:
Enter number:1892
The number of digits in the number is: 4



Powered by Blogger.