DEV Community

Subash
Subash

Posted on

Python Number Programs: Count Digits, Sum of Digits and Palindrome Number

These three programs use a while loop and basic arithmetic operators to perform different operations on a number.

1. Count the Number of Digits

This program counts how many digits are present in a given number.

Code:

no = 7654
count_of_digits = 0

while no > 0:
    no = no // 10
    count_of_digits += 1
else:
    print(count_of_digits)
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • while no > 0: The loop runs until the number becomes 0.
  • no // 10: Removes the last digit from the number.
  • count_of_digits += 1: Increments the counter by 1 for every digit removed.
  • Once the number becomes 0, the loop ends and prints the total digit count.

Output:

4
Enter fullscreen mode Exit fullscreen mode

2. Sum of Digits

This program extracts the last digit from a number and adds it to a new number.

Code:

no = 7654
count_of_digits = 0
sum_of_digits = 0

while no > 0:
    sum_of_digits = (sum_of_digits * 10) + no % 10
    no = no // 10
    count_of_digits += 1
else:
    print("Count of Digits", count_of_digits)
    print("Sum of Digits is", sum_of_digits)
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • no % 10: Extracts the last digit of the number.
  • sum_of_digits * 10: Shifts the existing number one position to the left.
  • + no % 10: Adds the extracted digit to the new number.
  • no // 10: Removes the last digit from the original number.
  • count_of_digits += 1: Counts each digit processed.

Note: Although the variable is named sum_of_digits, this code actually reverses the number. To calculate the actual sum of digits, use:

sum_of_digits = sum_of_digits + no % 10
Enter fullscreen mode Exit fullscreen mode

Output of the original code:

Count of Digits 4
Sum of Digits is 4567
Enter fullscreen mode Exit fullscreen mode

Output after the correction:

Sum of Digits is 22
Enter fullscreen mode Exit fullscreen mode

3. Palindrome Number

A palindrome number is a number that remains the same when its digits are reversed.

For example: 12321, 121 and 1331.

Code:

no = 12321
no2 = no
count_of_digits = 0
reverse = 0

while no > 0:
    reverse = (reverse * 10) + no % 10
    no = no // 10
    count_of_digits += 1
else:
    print("Count of Digits", count_of_digits)
    print("Reversed number is", reverse)

print("After loop", no)

if no2 == reverse:
    print("Palindrome")
else:
    print("Not Palindrome")
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • no2 = no: Stores the original number because the value of no changes inside the loop.
  • no % 10: Extracts the last digit.
  • reverse * 10: Shifts the existing reversed number one position to the left.
  • + no % 10: Adds the extracted digit to the reversed number.
  • no // 10: Removes the last digit from the original number.
  • count_of_digits += 1: Counts the number of digits.
  • if no2 == reverse: Compares the original number with the reversed number.

If both numbers are equal, it is a palindrome. Otherwise, it is not.

Output:

Count of Digits 5
Reversed number is 12321
After loop 0
Palindrome
Enter fullscreen mode Exit fullscreen mode

Top comments (0)