In this, you'll get to learn how to find the sum of n natural numbers in python (using different-different approaches) where the value of n is entered by a user at the runtime. i.e. Let's say if the user entered 5, the output should be 15
(1+2+3+4+5=15).
This covers four different approaches:
This is an example to find the sum of n natural numbers using for loop. Where the value of n is entered by the user at the runtime. If the user enters the value of n as 5, then this block of code executes the addition operation in the range of 5 using for loop.
print("Enter the Value of n: ")
n = int(input())
if n < 0:
print("\nInvalid Input!")
else:
sum = 0
for i in range(1, n+1):
sum += i
print(f"\nThe Sum of {n} natural numbers = {sum}")
Here, the f-string is a string literal that is used as an f at the beginning and curly braces containing expression that will be replaced with its value.
\n
indicates the newline character.
+=
in sum += i
represents the addition assignment(+=) operator, and that line is equivalent to sum = sum + i
.
Output:
Enter the Value of n:
10
The Sum of 10 natural numbers = 55
This example is to find the sum of n natural numbers that is the same as above but using while loop instead.
n = int(input("Enter the Value of n: "))
sum = 0
i = 1
while i<=n:
sum += i
i += 1
print(f"\nThe Sum of {n} natural numbers = {sum}")
Output:
Enter the Value of n: 10
The Sum of 10 natural numbers = 55
This example is to find the sum of numbers in Python using the list. In this, every input number is added into a list (num_list) one by one. Then the numbers in a num_list are added using for loop.
def sum_natural(num):
total = 0
for i in range(1, num+1):
total += i
return total
n = int(input("Enter the Value of n: "))
if n < 0:
print("\nInvalid Input!")
else:
# Function call to get sum of n natural numbers
sum = sum_natural(n)
print(f"\nThe Sum of {n} natural numbers = {sum}")
Here, the function sum_natural()
takes one argument, then it executes to get sum of the numbers using for loop in the range of 1 to num+1
as range() doesn't include upper limit. Then the result is stored in a variable total and returned from the function.
Output:
Enter the Value of n: 10
The Sum of 10 natural numbers = 55
This program finds the sum or n natural numbers using class.
All properties of the class named NaturalNumbers get assigned to an object named sum. Then the object can be used to access the member function of class NaturalNumbers using dot (.) operator. (sum.sum_natural(n))
class NaturalNumbers:
def sum_natural(self, num):
total = 0
for i in range(1, num+1):
total += i
return total
n = int(input("Enter the Value of n: "))
if n < 0:
print("\nInvalid Input!")
else:
# Function call to get sum of n natural numbers
sum = NaturalNumbers()
print(f"\nThe Sum of {n} natural numbers = {sum.sum_natural(n)}")
Output:
Enter the Value of n: 10
The Sum of 10 natural numbers = 55
In this, we discussed different examples for different methods to find the sum of n natural numbers in Python -- using for loop
, using while loop
, using a function
, and using class
.