Python program to find the sum of n numbers
Published on Aug. 22, 2023, 12:16 p.m.
Here is a simple Python program that finds the sum of n
numbers:
n = int(input("Enter the value of n: "))
sum = 0
for i in range(1, n+1):
sum += i
print("The sum of first", n, "numbers is", sum)
In this program, we first take the input for the number of values n
that we want to find the sum of. We then initialize a variable called sum
to 0. We use a for loop that iterates through the range 1 to n+1
and sums up the values using the +=
operator. Finally, we print out the sum of the n
numbers.
For example, if the user enters 5
, the program will output The sum of first 5 numbers is 15
.