Python Program to count the digits in a number

Write a Python program that takes a number as input and counts how many digits are present in that number. This program demonstrates working with loops and basic arithmetic to manipulate digits.

Input-Output Examples

plaintext
Example 1:
Input: Enter a number: 12345
Output: The number of digits in 12345 is: 5

Example 2:
Input: Enter a number: 9876543210
Output: The number of digits in 9876543210 is: 10

Algorithm to to count the digits in a number

  1. Start
  2. Take input: Prompt the user to enter a number.
  3. Handle negative numbers: Convert the number to its absolute value to count digits without considering the sign.
  4. Count the digits using a loop:
    • Repeatedly divide the number by 10 until it becomes zero.
    • For each division, increment the digit count.
  5. Display the result: Print the number of digits counted.
  6. End

Python Program to count the digits in a number (Using Loop)

python

# Python program to count the number of digits in a number using a loop

# Take input from the user
num = int(input("Enter a number: "))

# Handle negative numbers by taking the absolute value
num = abs(num)

# Initialize digit count to 0
digit_count = 0

# Loop to count the digits
while num > 0:
    num //= 10  # Remove the last digit
    digit_count += 1  # Increment digit count

# If the number is 0, count it as one digit
if digit_count == 0:
    digit_count = 1

# Display the result
print(f"The number of digits is: {digit_count}")

Python Program to count the digits in a number (Using **len()** with String Conversion)

python
# Python program to count the number of digits using len() and string conversion

# Take input from the user
num = input("Enter a number: ")

# Handle negative numbers by removing the minus sign
if num[0] == '-':
    num = num[1:]

# Count the number of digits using len()
digit_count = len(num)

# Display the result
print(f"The number of digits is: {digit_count}")

Code Explanation

  1. Taking input from the user:
    The program uses the input() function to take a number from the user. In the first method, the input is converted to an integer, while in the second method, the input is treated as a string.
  2. Handling negative numbers:
    • In the first method: The abs() function is used to convert negative numbers to their absolute values so that the sign is ignored when counting digits.
    • In the second method: The minus sign ('-') is removed by slicing the string if the input number is negative.
  3. Counting digits using loops:
    In the first method, a while loop is used to repeatedly divide the number by 10 to remove the last digit until the number becomes zero. For each iteration, the digit count is incremented.
  4. Counting digits using **len()** and string conversion:
    In the second method, the number is converted to a string, and the length of the string (ignoring the minus sign for negative numbers) is used to count the digits.
  5. Displaying the output:
    The print() function is used to display the number of digits in the input number.

Python

6239

482

Related Articles