+ 1
Sum of Consecutive Numbers
Take a number N as input and output the sum of all numbers from 1 to N (including N). Sample Input 100 Sample Output 5050 Explanation: The sum of all numbers from 1 to 100 is equal to 5050. You can iterate over a range and calculate the sum of all numbers in the range. Remember, range (a, b) does not include b, thus you need to use b+1 to include b in the range.
6 Respuestas
+ 7
Taha Nekhli ,
# basic version using a for loop
number = int(input())
total = 0
for num in range(1, number +1):
total += num
print(total)
# version using sum() built-in function
number = int(input())
print(sum(range(1, number +1)))
+ 5
Taha Nekhli
what does this [x+1:] here
Why not to add x in a temp variable like total
total = total + x
range is already a function so you can't use as a variable so do this:
for x in range (a, b + 1):
+ 3
Hello. Do you have any attempts?
Try to use **for** loop and **range** function
+ 2
N = int(input())
#your code goes here
a = 1
b = N
range = [a,b+1]
for x in range :
print([x+1:])
I tried this but it doesn’t work
+ 2
n = int(input())
print(n*(n+1)/2)
Learn mathematics if you want to do so solutions..
- 1
Hi guys please a solution for this thaanx