Menu

Python Day 15: Count, Sum, Reverse and Check Palindrome Numbers
πŸ“°

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

DEV Community: beginnersΒ·Vasanthakumari NΒ·8 days ago
#jyvOwPZG
#devtobeginners#devto#dev#number#reverse
Long article (668 words) β€” no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.