Sum, Count, and Reverse of Digits in Python (While Loop & Recursion)
<p><strong>1. Sum of digits</strong></p> <p><strong>Iterative Approach (Using While Loop)</strong><br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="n">no</span> <span class="o">=</span> <span class="nf">int</span><span class="p">(</span><span class="nf">input</span><span class="p">(</span><span class="sh">"</span><span class="s">Enter No: </span><span class="sh">"</span><span class="p">))</span> <span class="nb">sum</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">while</span> <span class="n">no</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span> <span class="nb">sum</span> <span class="o">=</span> <span class="nb">sum</span> <span class="o">+</span> <span class="n">no</span> <span cla
- Sum of digits
Iterative Approach (Using While Loop)
while no > 0: sum = sum + no % 10 no = no // 10 else: print(sum)`
Enter fullscreen mode
Exit fullscreen mode
Recursive Approach
Enter fullscreen mode
Exit fullscreen mode
Output
- Count of digits
Iterative Approach (Using While Loop)
while num > 0: num = num // 10 count += 1
print("Count =", count)`
Enter fullscreen mode
Exit fullscreen mode
Recursive Approach
print(count(1234))`
Enter fullscreen mode
Exit fullscreen mode
Output
- Reverse a Number
Iterative Approach (Using While Loop)
while num > 0: rev = rev * 10 + num % 10 num = num // 10*
print("Reverse =", rev)`
Enter fullscreen mode
Exit fullscreen mode
Recursive Approach
Enter fullscreen mode
Exit fullscreen mode
Output
DEV Community
https://dev.to/harini_magesh_fa40041cf8d/sum-count-and-reverse-of-digits-in-python-while-loop-recursion-29jkSign in to highlight and annotate this article

Conversation starters
Daily AI Digest
Get the top 5 AI stories delivered to your inbox every morning.
Knowledge Map
Connected Articles — Knowledge Graph
This article is connected to other articles through shared AI topics and tags.


Discussion
Sign in to join the discussion
No comments yet — be the first to share your thoughts!