How to Calculate the Sum of N Natural Numbers in Python


In this example, we will learn how to calculate the sum of N natural numbers.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

n = int(input("Enter a natural number: "))
sum1 = 0
for i in range(1, n+1):
    sum1 += i
print("The sum of first n natural numbers is", sum1)

Output:

Enter a natural number: 10
The sum of first n natural numbers is 55

Enter a natural number: 55
The sum of first n natural numbers is 1540
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement