Robotics & Artificial Intelligence

Python allows repetitive execution of a block of code through loops. The two primary loops in Python are for and while. You may use the range() function with a for loop to iterate over a sequence of numbers.

Write Python code that calculates the sum of all even numbers between 1 and 100 using for loop.

Python Control Flow

3 Likes

Answer

sum_even = 0
for i in range(1, 101):
    if i % 2 == 0:
        sum_even = sum_even + i
print("Sum of all even numbers between 1 and 100 =", sum_even)

Output

Sum of all even numbers between 1 and 100 = 2550

Answered By

1 Like


Related Questions