Categories: Uncategorized

Python program to sum of Natural number from 1 to n|4 ways

Python program to sum of Natural number from 1 to n

In this tutorial, we will discuss the Python program to the sum of Natural number from 1 to n

In this post, we are going to learn how to find the sum of natural numbers in python language in different 4 ways.

Addition of Natural number

Program to the sum of Natural number – using for loop

Program 1

#Python program to find sum of natural numbers 1 to givrn number
n=input("Enter the number to calculate sum: ")
n=int(n)
sum=0;
for num in range(0,n+1,1):
    sum=sum+num

print("Sum of 1 to ",n,"numbers is: ",sum)

When the above code is executed it produces the following output

Enter the number to calculate sum: 25
Sum of 1 to  25 'numbers is:  325

This program allows the user to enter a maximum number. and it displays the sum of natural numbers from 1 to given number using for loop in Python language

Program to the sum of Natural number – using the while loop

Program 2

#Python program to find sum of natural numbers 1 to givrn number
num=input("Enter a positive number to calculate sum: ")
num=int(num)
sum=0;
while (num>0):
    sum=sum+num #sum+=num

    num-=1

print("Sum of 1 to ",num,"numbers is: ",sum)

When the above code is executed it produces the following output

Enter a positive number to calculate sum: 15
Sum of 1 to  15 numbers is:  120

This program allows the user to enter a maximum number. and it displays the sum of natural numbers from 1 to given number using while loop in Python language

Program to the sum of Natural number – using the function

Program 3

#Python program to find sum of natural numbers 1 to givrn number
def sum_Num(num): #function defivition
  if(num==0):
      return num
  else:
      return(num*(num+1)/2)
number=input("Enter a positive number to calculate sum: ")
number=int(number)
sum= sum_Num(number)#function call

print("Sum of Natural numbers from 1 to {0}={1}".format(number,sum))
#display the result

When the above code is executed it produces the following output

Enter a positive number to calculate sum: 25
Sum of Natural numbers from 1 to 25=325

This program allows the user to enter a maximum number. and it displays the addition of natural numbers from 1 to given number using the function in Python language

Program to the sum of Natural number – using the recursion

Program 4

#Python program to find sum of natural numbers 1 to givrn number
def sum_NumRec(num):#recursive function definition
  if(num==0):
      return num
  else:
      return(num+sum_NumRec(num-1))
number=input("Enter a positive number to calculate sum: ")
number=int(number)
sum= sum_NumRec(number)#function call

print("Sum of Natural numbers from 1 to {0}={1}".format(number,sum))

When the above code is executed it produces the following output

Enter a positive number to calculate sum: 100
Sum of Natural numbers from 1 to 100=5050

This program allows the user to enter a maximum number. and it displays the addition of natural numbers from 1 to given number using the recursive function in Python language

 

Suggested for you

for loop in Python

while loop in python

if statements in Python

function in Python

recursion of Python

 

Similar post

Java program to Sum of natural numbers 1 to n |5 ways

C++ program to sum of Natural number from 1 to n

C program to sum of Natural number from 1 to n

 

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.