Categories: category

Calculate the sum of natural numbers in Python

Calculate the sum of natural numbers in Python

In this tutorial, we will discuss a concept of Python program to calculate the sum of natural numbers using loops

In this article, we are going to learn  how to  find the sum of natural numbers using loops in the Python programming language

Sum of natural numbers in Python

Python program to find the sum of natural numbers using loop

Python code to find the sum of numbers using for loop

Program 1

This program takes input from the user and stores in variable num. Then, the for loop is used to calculate the sum of natural numbers up to the given number.

 

#Python program to calculate sum of natural numbers
num=input("Enter numbet to calculate sum: ")
#received input from the user
num=int(num)
sum=0;

for number in range(0,num+1,1):
    sum=sum+number
print("Sum of first ",num,"natural numbers is:",sum )

When the above code is executed, it produces the following results

Enter numbet to calculate sum: 20
Sum of first 20 natural numbers is: 210

 

Program 2

This program takes input from the user and stores in variable num. Then, the while loop is used to calculate the sum of natural numbers up to the given number

#Python program to calculate sum of natural numbers
num=input("Enter numbet to calculate sum: ")
num=int(num)
sum=0;

if num<0:
    print("Enter a positive number")

    
else:
  while(num>0):
     sum=sum+num
     num-=1;
print("Sum is:",sum )

When the above code is executed, it produces the following results

Enter numbet to calculate sum: 10
Sum is: 55

Similar post

C++ program to find the sum of natural numbers using loops

Java  program to find the sum of natural numbers using loops

C program to find the sum of natural numbers using loops

 

 

Suggested for you

for loop in Python  language

while loop in Python language

 

 

 

 

 

Python program to find the sum of two numbers using recursion
Java program to find the sum of natural numbers using recursion
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago