In this program, we will find the sum of even numbers between 1 to n.
Steps to solve the program
- Use for loop with range function to iterate over numbers between 1-n.
- Create a variable total and assign its value equal to 0.
- During the iteration check, if the number is even and add only those numbers to the total variable.
- Print the output.
n= int(input("Enter the last number: "))
total = 0
for i in range(1,n+1):
if i%2 == 0:
total += i
print(total)
Output :
30