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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago