From c780f5a641f45fe0d9d62af670399852c20e8bbd Mon Sep 17 00:00:00 2001 From: Anika Kamath <87904385+anika-kamath@users.noreply.github.com> Date: Sat, 9 Jul 2022 21:01:28 +0530 Subject: [PATCH] chore(Python): add sum of n numbers using recursion (#770) --- algorithms/Python/README.md | 1 + recursive-sum-of-n-numbers.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 recursive-sum-of-n-numbers.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index bc9438e9..f3fb531a 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -25,6 +25,7 @@ - [Factorial](recursion/factorial.py) - [n-th Fibonacci number](recursion/nth_fibonacci_number.py) - [Recursive Insertion Sort](recursion/recursive_insertion_sort.py) +- [Recursive Sum of n numbers](recursion/recursive-sum-of-n-numbers.py) ## Scheduling - [Interval Scheduling](scheduling/interval_scheduling.py) diff --git a/recursive-sum-of-n-numbers.py b/recursive-sum-of-n-numbers.py new file mode 100644 index 00000000..9e7014ac --- /dev/null +++ b/recursive-sum-of-n-numbers.py @@ -0,0 +1,12 @@ +def recsum(n): + if n<=1: + return(n) + else: + return(n+recsum(n-1)) + +n= 15 + +if n<0: + print("Enter a positive integer") +else: + print("Sum =",recsum(n))