Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Python Program to Find the Sum of Natural Numbers

    Python Programming Examples


    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

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.