Python Program to Find the Sum of Natural Numbers

By | October 3, 2021
Python Program to Find the Sum of Natural Numbers

Here in this article, we have provided a python program that can find the sum of n natural numbers using Python loop.

Prerequisite topics to create this Program

  • Python Input, Output
  • Python if…else statement
  • Python Data types
  • Python Operators
  • Python Loop

Steps

  • First, we ask the user to enter a number n.
  • Using the int() function we will convert the entered string into an integer value.
  • Now using the for loop we will go through the range 1 to the number n+1, add all the numbers and print sum as an output.

Python Program to Find the Sum of Natural Numbers:

Code:

n = int(input("Enter the number:" ))
sum=0
if n > 1:
    for i in range(1,n+1):
        sum+=i
    print("The sum of all natural number upto",n,"is ",sum)
else:
    print("Please Enter a value greater than 1")

Output 1:

Enter the number:2
The sum of all natural numbers upto 2 is 3

Output2:

Enter the number:20
The sum of all natural numbers upto 20 is 210

People are also reading:

Vamware
  1. Python Program to Display Calendar
  2. Perfect Number in C
  3. Python Program to Check Whether a String is Palindrome or Not
  4. Python Program to Check if a Number is Odd or Even
  5. WAP to swap two numbers
  6. Python Program to Convert Celsius To Fahrenheit
  7. WAP in C++ & Python to Insert an Element in an Array
  8. Python Program to Solve Quadratic Equation
  9. WAP in C++ & Python to Calculate Compound Interest
  10. WAP to calculate the sum of two numbers
Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.