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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.