-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.sum_of_digit_of_a_number.py
More file actions
54 lines (41 loc) · 954 Bytes
/
10.sum_of_digit_of_a_number.py
File metadata and controls
54 lines (41 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 10.Sum of digit of a number
# using Sum function
num="123"
abc=list(map(int,num))
print("Sum of the str: ",sum(abc))
# Using String Character Extraction
num=input("enter the number: ")
sum=0
for i in num:
sum+=int(i)
print(sum)
# Using Brute Force
num=int(input("enter the number: "))
sum=0
while num!=0:
digit=num%10
sum+=digit
num=num//10
print(sum)
# Using Recursion I
def sum_of_digit(num,sum):
if num==0:
return sum
digit=num%10
sum+=digit
return sum_of_digit(num//10,sum)
num=int(input("enter the number: "))
sum=0
print(sum_of_digit(num,sum))
# Using Recursion II
def sum_of_digit(num):
if num==0:
return 0
return num%10+sum_of_digit(num//10)
num=int(input("enter the number: "))
print(sum_of_digit(num))
# Using One Line Recursion
def sum_of_digit(num):
return 0 if num==0 else num%10 +sum_of_digit(num//10)
num=int(input("enter the number: "))
print(sum_of_digit(num))