Python program to find sum of n natural numbers; Through this tutorial, i am going to show you how to find the sum of the first n natural number using while loop, for loop, and recursion function.
Formula for Find or Calculate Sum of N Natural Numbers = [n(n+1)]/2 .
Python Programs to Find/Calculate Sum Of n Natural Numbers
There are three different ways to find sum of n natural numbers in python; as shown below:
- Python Program to Calculate Sum of N Natural Numbers using While Loop
- Python Program to find Sum of N Natural Numbers using For Loop
- Python Program to calculate Sum of N Natural Numbers using Recursion Function
Python Program to Calculate Sum of N Natural Numbers using While Loop
- Get input number from the user in program
- Iterate while loop and calculate sum of n natural number
- As well as store value in variable
- Print sum of n natural number
# Python Program to find Sum of N Natural Numbers number = int(input("Please Enter any Number: ")) total = 0 value = 1 while (value <= number): total = total + value value = value + 1 print("The Sum of Natural Numbers from 1 to {0} = {1}".format(number, total))
Output
Please Enter any Number: 5 The Sum of Natural Numbers from 1 to 5 = 15
Python Program to find Sum of N Natural Numbers using For Loop
- Get input number from the user in program
- Iterate for loop and calculate sum of n natural number
- As well as store value in variable
- Print sum of n natural number
# Python Program to find Sum of N Natural Numbers number = int(input("Please Enter any Number: ")) total = 0 for value in range(1, number + 1): total = total + value print("The Sum of Natural Numbers from 1 to {0} = {1}".format(number, total))
Output
Please Enter any Number: 10 The Sum of Natural Numbers from 1 to 10 = 55
Python Program to calculate Sum of N Natural Numbers using Recursion Functions
- Get input number from the user in program
- Define a function, which is calcuate sum of n natural number
- As well as store value in variable
- Print sum of n natural number
# Python Program to find Sum of N Natural Numbers def sum_of_n_natural_numbers(num): if(num == 0): return num else: return (num + sum_of_n_natural_numbers(num - 1)) number = int(input("Please Enter any Number: ")) total_value = sum_of_n_natural_numbers(number) print("Sum of Natural Numbers from 1 to {0} = {1}".format(number, total_value))
Output
Please Enter any Number: 10 The Sum of Natural Numbers from 1 to 10 = 55
Recommended Python Tutorials
Recommended:-Python Program to Print All Timezones
Recommended:-Python Count the Occurrences in an Array
Recommended:-Python Program to Print Binary Numbers From 1 to N
Recommended:-Python Program to print Elements in a List
Recommended:-Python Program to Swap Characters of Given String
Recommended:-Swap First Two Characters in Each String in Python
Recommended:-Python Split String into Array of Characters
Recommended:-Python Program to Reverse String Using Stack
Recommended:-Python Insert Element at Specified Index in List
Recommended:-Python Program to Swap Two Elements in a List
Recommended:-How to Take Input List From User in Python
Recommended:-Python Program to Find LCM of Two Numbers
Recommended:-Python Add and Remove Elements From List
Recommended:-Python Find Difference of Two Lists
Recommended:-Python Program to Split Even and Odd Numbers in List