DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on

Python Day 15: Count, Sum, Reverse and Check Palindrome Numbers

1. Count the Digits

Problem: Count the total number of digits in a given number.

For example:

Number: 7654
Number of digits: 4

Step 1: Understand the Logic

We can remove the last digit of a number using integer division (// 10).

no = 7654

no = no // 10  # 765
no = no // 10  # 76
no = no // 10  # 7
no = no // 10  # 0

Enter fullscreen mode Exit fullscreen mode

Every time we remove a digit, we increase the counter by 1.

Step 2: Using a While Loop

no = 7654
count_of_digits = 0

while no > 0:
    no = no // 10
    count_of_digits += 1

print("Count of Digits:", count_of_digits)
Enter fullscreen mode Exit fullscreen mode

Output:

Count of Digits: 4
Enter fullscreen mode Exit fullscreen mode

2. Sum of Digits

Problem: Calculate the sum of all digits in a given number.

For example:

Number: 7654

7 + 6 + 5 + 4 = 22

Understanding the Logic

We can extract the last digit using the modulus operator (% 10).

no = 7654

print(no % 10) # 4

To get the next digit, we must remove the last digit using // 10.

no = 7654
sum_of_digits = 0

while no > 0:
    digit = no % 10
    sum_of_digits = sum_of_digits + digit
    no = no // 10

print("Sum of Digits:", sum_of_digits)
Enter fullscreen mode Exit fullscreen mode
Output:

Sum of Digits: 22
Enter fullscreen mode Exit fullscreen mode

3. Reverse a Number

Problem: Reverse the digits of a given number.

For example:

Original Number: 7654
Reversed Number: 4567

no = 7654
reverse = 0

while no > 0:
    reverse = (reverse * 10) + (no % 10)
    no = no // 10

print("Reversed Number:", reverse)
Enter fullscreen mode Exit fullscreen mode
Output:

Reversed Number: 4567
Enter fullscreen mode Exit fullscreen mode

Understanding the Logic

The important formula is:

reverse = (reverse * 10) + (no % 10)

We multiply the existing reverse value by 10 and add the newly extracted digit.

4.Check Whether a Number Is a Palindrome

Problem: A palindrome number reads the same from left to right and right to left.

Examples:

  • 121 → Palindrome
  • 12321 → Palindrome
  • 123 → Not a Palindrome
  • 456 → Not a Palindrome

no = 12321
original = no
reverse = 0

while no > 0:
    reverse = (reverse * 10) + (no % 10)
    no = no // 10

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

Palindrome
Enter fullscreen mode Exit fullscreen mode

Understanding the Logic

  • Store the original number in another variable.
  • Reverse the given number.
  • Compare the original number with the reversed number.
  • If both are equal, it is a palindrome.
  • Otherwise, it is not a palindrome.

5.Handling Negative Numbers Using abs()

What happens if the input is a negative number?

For example:

no = -123

We can convert it into a positive number using Python's built-in abs() function.

no = -123

print("Before abs:", no)

no = abs(no)

print("After abs:", no)

Enter fullscreen mode Exit fullscreen mode
Output:

Before abs: -123
After abs: 123
Enter fullscreen mode Exit fullscreen mode

Another way to convert a negative number into a positive number:

if no < 0:
no = no * -1

Palindrome Check with Negative Numbers

no = -12321
original = no
no = abs(no)

reverse = 0

while no > 0:
    reverse = (reverse * 10) + (no % 10)
    no = no // 10

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

Output:

Palindrome
Enter fullscreen mode Exit fullscreen mode

Top comments (0)