Skip to content
Dev.to1 min read

Sum, Count, and Reverse of Digits in Python...

1. Sum of digits Iterative Approach (Using While Loop) no = int(input("Enter No: ")) sum = 0 while no > 0: sum = sum + no % 10 no = no // 10 else: print(sum) Recursive Approach def sum_of_digits(no): if no == 0: # Base condition return 0 return (no % 10) + sum_of_digits(no // 10) no = int(input("Enter No: ")) result = sum_of_digits(no) print(result) Output 2. Count of digits Iterative Approach (Using While Loop) num = int(input("Enter number: ")) count = 0 while num > 0: num = num // 10 count +=
Read original on dev.to
0
0

Comment

Sign in to join the discussion.

Loading comments…

Related

Get the 10 best reads every Sunday

Curated by AI, voted by readers. Free forever.

Liked this? Start your own feed.

0
0