Python ProgrammingPython Programming

Calculate Sum of the First N Positive Integers

Write a small python program that reads a positive integer (N) from the user and then displays the sum of all of the integers from 1 to N. The sum of the first N positive integers can be computed using the formula: sum = (n)(n + 1)/2.
##
#Calculate the sum of first N positive integers.
##

#Read the input from the user
n = int(input("Enter a positive integer: "))

#Calculate the sum
total = n * (n+1) / 2

#Display the result
print("The sum of the first",n,"positive integers",total)
Sample output of above program.
C:\>python C:\Python\programs\program1.py
Enter a positive integer: 10
The sum of the first 10 positive integers 55.0

C:\>