Categories: category

Python program to find the sum of natural numbers using recursion

Python program to find the sum of natural numbers using recursion

In this tutorial, we will discuss a concept of the  Python program to find the sum of natural numbers using recursion

In this article, we are going to learn  how to  find sum of natural numbers using recursion in the Python  programming language

Fnd the sum of natural numbers

What are Natural numbers

The positive integer numbers 1,2,3,4….n are known as natural numbers

Program 1

This program allows defining a number to find the sum of natural numbers from 1 to given number using the recursive function in Python programming language.

#Python program to find the sum of natural numbers up to given number using recursive function
def sum_Num(n):
    if n<= 1:
        return n
    else:
        return n+ sum_Num(n-1)

num=16 #you can change this value for different result

if num < 0:
    print("Enter a positive number")
else:
    print("The sum is :",sum_Num(num))

When the above code is executed, it produces the following results

The sum is : 136

 

Program 2

This program allows to enter a number to find addition of natural numbers from 1 to given number using recursive function in Python programming language

#Python program to find the sum of natural numbers up to given number using recursive function
def sum_Num(n):
    if n<= 1:
        return n
    else:
        return n+ sum_Num(n-1)

num=int(input("Enter a number: "))

if num < 0:
    print("Enter a positive number")
else:
    print("The sum is :",sum_Num(num))

When the above code is executed, it produces the following results

Enter a number: 20
The sum is : 210

 

Java program to find the sum of natural numbers using loops

Python program to Calculate the sum of natural numbers using loops

C++ program to calculate the sum of natural numbers using loops

C program to calculate the sum of natural numbers using loops

 

Java program to find the sum of natural numbers using recursion

C program to Calculate the sum of natural numbers using recursion

C++ program to calculate the sum of natural numbers using recursion

 

 

Suggested for you

Python if else statements

Python function

Python recursion

Java program to find the sum of natural numbers using recursion
Find product of two numbers using recursion in Python
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

20 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago