Calculate sum of first n natural numbers Dry Run in PYTHON

Calculate sum of first n natural numbers is an interactive PYTHON dry run visualizer from the For programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Calculate sum of first n natural numbers Program Code

# Sum of first n natural numbers
n = int(input("Enter a number: "))

sum = 0
for i in range(1, n + 1):
    sum += i

print(f"Sum of first {n} natural numbers = {sum}")

# Verification using formula: n(n+1)/2
formula_sum = n * (n + 1) // 2
print(f"Using formula: {formula_sum}")

View the complete For programs page.

Program Console Calculate sum of first n natural numbers Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.