1.

Write a program to find the sum of all even numbers from 1 to n where the value of n is taken as input. [for example when n is 10 then the sum is 2+4+6+8+10 = 30] using only two variables

Answer»

PROGRAM:

n = INT(input("ENTER a number"))

SUM = 0

for i in range (1,(n+1),1):

   if int(i % 2) == 0:

       sum = sum + i

print("The sum is : ")

print(sum)

Output

Enter a number10

The sum is :  

30

Language

Python 3

-----Hope this helps   :)



Discussion

No Comment Found