Learn about Find the Sum of N Natural Numbers using Function Python in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.
Find the Sum of N Natural Numbers using Function Python
findSum() in the below program takes a number as an argument and returns the total sum of the numbers.
# Python program to find sum of n natural numbers using function #user-defined function def findSum(num): sum = 0 x = 1 while x <= num: sum += x x += 1 return sum # input num = int(input('Enter a number: ')) # print result print('The sum of natural number =', findSum(num))
Enter a number: 6
The sum of natural number = 21
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.