Finding the sum of all even numbers upto a given number

How to find the sum of all even numbers upto a given number in Python ?

To find the sum of all even numbers upto a given number we can use the following python code. 

#taking a number from user upto which sum is to be find.
n = int(input("enter a number"))

#setting the base value of sum
sum = 0

#setting the range
for i in range (1,n+1):
#checking for even number in range
if i % 2 == 0:
#incrementing the value of sum
sum = sum + i

#printing sum
print (sum)

Comments

Popular posts from this blog

FINDING FACTORIAL OF A NUMBER IN PYTHON