Sum of an arithmetic series
n = int(input("Enter a number: "))
# Using the formula for the sum of an arithmetic series
sum_of_n_numbers = (n * (n + 1)) // 2
print("Sum of the first", n, "numbers is", sum_of_n_numbers)
In this program, we first take the input `n` from the user. Then we use the formula for the sum of an arithmetic series, which is `(n * (n + 1)) // 2`, to calculate the sum of the first `n` numbers. Finally, we print the result.
Comments
Post a Comment