深度阅读

Python program to find the sum of n numbers

作者
作者
2023年08月22日
更新时间
6.26 分钟
阅读时间
0
阅读量

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.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。