Table of Contents
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.
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
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