Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

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

Posted on October 19, 2019October 19, 2019

Table of Contents

  • Python program to sum of Natural number from 1 to n
    • Program to the sum of Natural number – using for loop
    • Program to the sum of Natural number – using the while loop
    • Program to the sum of Natural number – using the function
    • Program to the sum of Natural number – using the recursion

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.

Python program to sum of Natural number from 1 to n
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

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes