DEV Community

Cover image for Crack Number Problems: Sum of Digits, Count, and Reverse
Arul .A
Arul .A

Posted on

Crack Number Problems: Sum of Digits, Count, and Reverse

Introduction :
When you start programming, most people jump into big concepts and ignore the basics. That’s a mistake.

Simple number problems like:

  • Sum of digits
  • Count of digits
  • Reverse a number

These are not “easy” problems — they build your core logic. If you struggle here, you’ll struggle everywhere else.

  1. Sum of Digits :

flow chart :

In Python Recursion:

def sum(no):
    if no == 0:  
        return 0
    else:
        return (no % 10) + sum(no // 10)
print(sum(1234))
Enter fullscreen mode Exit fullscreen mode

-In python Loop :

Sum = 0
no = 1234
while no>0:
    Sum = Sum + no%10
    no = no//10
print(Sum)
Enter fullscreen mode Exit fullscreen mode

2.Count Of Digits:

In Python Recursion:

def count(no):
    if no < 10:   
        return 1
    else:
        return 1 + count(no // 10)
print(count(1234))
Enter fullscreen mode Exit fullscreen mode

-In python Loop :

Sum = 0
no = 1234
count = 0
if no == 0:
    count = 1
while no>0:
    Sum = Sum + no%10
    no = no//10
    count+=1
print(count)

Enter fullscreen mode Exit fullscreen mode

3.Reverse a number:

In Python Recursion:

def reverse(no, rev=0):
    if no == 0:  
        return rev
    else:
        return reverse(no // 10, rev * 10 + no % 10)
print(reverse(1234))
Enter fullscreen mode Exit fullscreen mode

-In python Loop :

no = 1234
reverse = 0
while no>0:
    reverse = reverse*10 + no%10
    no = no//10
print(reverse)

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam

Good that you have flow chart here. Try JS and Java code as well.

Collapse
 
aj_arul profile image
Arul .A

ok sir i will try...