Articles

Program to find sum of n natural numbers in python

Program to find sum of n natural numbers in python


In this Python programming guide, we are going to learn:

  • write a program to find the sum of n natural numbers in python

Python program to find sum of n natural numbers

The python program to find sum of n natural numbers using for loop is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

num = int(input("Enter a natural number: "))
sum_natural = 0

if num < 1:
    print("Wrong Input.")
else:
    for i in range(num+1):
        sum_natural += i

print("The sum is=",sum_natural)

The output of program to calculate sum of first n natural numbers in python is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter a natural number: 10
The sum is= 55

Few important tips about this program

1. The natural numbers begins from 1 and goes to positive infinity.

2. Here, we take the user input and convert it to integer data type. This is the upper limit of our range program.

3. We find the sum of numbers from 1 upto the user input and display the result on the terminal.

 

Program to find sum of n natural numbers in python snapshot:

write a program to find the sum of n natural numbers in python,

 


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast
Page Views :    Published Date : Mar 11,2021  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!