+ 2
How to calculate 1 too 100 addition ?
I can't calculat this !
3 Answers
+ 6
Something like this would work:
total = 0
for i in range(1, 100):
total += i
print(total)
That would be i from 1 to 99 including 1 and including 99.
range(1, 101) would be needed if you want 100 included in that loop.
If this wasn't just a lesson with Python and you want a more efficient solution, consider how that total is related to area of a right-angle triangle. sum of 1 to 100(excluding 100, including 1) is equal to 100 * (100 - 1) / 2. In other words, the same number can be calculated without a loop even when the limit of 100 is an unknown like x * (x - 1) / 2.
+ 4
By Using SUM of AP(arithmetic mean)
S = n/2[2a + (n â 1) Ă d]
where
n= 100
a = 1
d= 1
S = 100/2[2(1)+ (100 â 1) Ă 1]= 50(101)=5050
+ 1
Thanks sar