Find the sum of even numbers between 1 to n

In this program, we will find the sum of even numbers between 1 to n.

Steps to solve the program
  1. Use for loop with range function to iterate over numbers between 1-n.
  2. Create a variable total and assign its value equal to 0.
  3. During the iteration check, if the number is even and add only those numbers to the total variable.
  4. 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
				
			

find the sum of all natural numbers between 1 to n

find the sum of all odd numbers between 1 to n

Leave a Comment