Table of Contents
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 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 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 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 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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.