Python Program to Find Sum of n Natural Numbers

In this program, First we create a list of n numbers and then use sum method to add them.

Example:--

num = int(input("Enter the value of n \n"))

if num < 0:
   print("Please enter a positive number")
else:
    total = sum([i for i in range(num+1)])
    print("The sum is", total)

OUTPUT:--
Enter the value of n 
23
The sum is 276

 

Total Views: 444