Python Programming Examples Tutorial Index

Python Number Programs

This Python example code demonstrates a simple Python program to find the sum of natural numbers and print the output to the screen. This program takes an integer input from the user and runs the loop from the taken number to zero, and adds the numbers during the loop.



Program:

#Taking user input
num = int(input('Enter a number:'))

if num < 0:
   num = input('Please enter a positive number:')
else:
   sum = 0
   #Loop to iterate till zero
   while(num > 0):
       sum += num
       num -= 1
   print("The sum of the natural numbers is:", sum)

Program Output:

Enter a number:15
The sum of the natural numbers is: 120


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram