Intermediate Level

Sum of Digits Program in Python

Aproch 01

ur_input = input("Enter Some Number: ") # return str
total_ = 0
for i in ur_input:
total_ += int(i)
print(total_)

Aproch 02

ur_input = int(input("Enter Some Number: ")) # return type is int
total_ = 0
while ur_input > 0:
last_num = ur_input % 10
total_ += last_num
ur_input = ur_input // 10
print(total_)

Sum of Digit of a Number using Recursion in Python


ur_input = int(input("Enter Some Number: "))
def digit_sum(num):
if num < 10: return num else: return (num%10+digit_sum(num//10)) res = digit_sum(ur_input) print(res)

Sum of of Numbers using lambda function


lst = [i for i in range(1, 10)] # [1,2,3,4,5,6,7,8,9,10]
from functools import reduce
sum_ = reduce(lambda x,y:x+y, lst)
print(sum_)

Python Program to Count the Number of Digits in a Number


ur_input = input("Enter Some Number: ")
from collections import Counter
res = dict(Counter(ur_input))
print(res)

Python Program to Find All the factors of a positive Integer


ur_input = int(input("Enter Some Number: "))
for i in range(2, ur_input+1):
if ur_input % i == 0:
print(i)