Skip to content

Commit c67e0d1

Browse files
Merge pull request #36 from codewithdhruba01/Add/Questions
Add recursive sum function for natural numbers
2 parents c4d78d1 + 69cdf9b commit c67e0d1

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

12_Recursion/sum.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Sum of First n Natural Numbers
2+
3+
def sum_n(n):
4+
if n == 0:
5+
return 0
6+
return n + sum_n(n - 1)
7+
8+
print(sum_n(5)) # Output: 15
9+
10+
## Har call me current number ko uske previous numbers ke sum me add karte hain.

0 commit comments

Comments
 (0)