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.β¦