Live
Black Hat USAAI BusinessBlack Hat AsiaAI BusinessGeopolitics, AI, and Cybersecurity: Insights From RSAC 2026Dark Readingbuilding an atomic bomberman clone, part 4: react vs. the game loopDEV CommunityWhy My "Lightning Fast" Spring Boot Native App Took 9 Seconds to Boot on Fly.ioDEV CommunityThis International Fact-Checking Day, use these 5 tips to spot AI-generated contentFast Company TechShow HN: A task market where AI agents post work, claim it, and build reputationHacker News AI TopA quiz that scores your job's AI replacement risk (Anthropic/ILO/OECD data)Hacker News AI TopHow I'm Using an AI Assistant to Offload the "Meta-Work" of My DayHacker News AI TopWhat distinguishes great engineers when AI writes the code?Hacker News AI TopCursor AI agent admits to deceiving user during 61GB RAM overflowHacker News AI TopOur AI agent tried to read our .env file 30 seconds inHacker News AI TopSuits Against Tempus AI Test Legal Lines for Mining Genetic DataHacker News AI TopBuilding HIPAA-Compliant Software for Dental Practices: What Developers Need to KnowDEV CommunityBlack Hat USAAI BusinessBlack Hat AsiaAI BusinessGeopolitics, AI, and Cybersecurity: Insights From RSAC 2026Dark Readingbuilding an atomic bomberman clone, part 4: react vs. the game loopDEV CommunityWhy My "Lightning Fast" Spring Boot Native App Took 9 Seconds to Boot on Fly.ioDEV CommunityThis International Fact-Checking Day, use these 5 tips to spot AI-generated contentFast Company TechShow HN: A task market where AI agents post work, claim it, and build reputationHacker News AI TopA quiz that scores your job's AI replacement risk (Anthropic/ILO/OECD data)Hacker News AI TopHow I'm Using an AI Assistant to Offload the "Meta-Work" of My DayHacker News AI TopWhat distinguishes great engineers when AI writes the code?Hacker News AI TopCursor AI agent admits to deceiving user during 61GB RAM overflowHacker News AI TopOur AI agent tried to read our .env file 30 seconds inHacker News AI TopSuits Against Tempus AI Test Legal Lines for Mining Genetic DataHacker News AI TopBuilding HIPAA-Compliant Software for Dental Practices: What Developers Need to KnowDEV Community
AI NEWS HUBbyEIGENVECTOREigenvector

Sum, Count, and Reverse of Digits in Python (While Loop & Recursion)

DEV Communityby HariniApril 2, 20262 min read1 views
Source Quiz

<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

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

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

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

Was this article helpful?

Sign in to highlight and annotate this article

AI
Ask AI about this article
Powered by Eigenvector · full article context loaded
Ready

Conversation starters

Ask anything about this article…

Daily AI Digest

Get the top 5 AI stories delivered to your inbox every morning.

Knowledge Map

Knowledge Map
TopicsEntitiesSource
Sum, Count,…recursiveDEV Communi…

Connected Articles — Knowledge Graph

This article is connected to other articles through shared AI topics and tags.

Building knowledge graph…

Discussion

Sign in to join the discussion

No comments yet — be the first to share your thoughts!