395,071 views
20 votes
20 votes
Write an algorithm to find the sum of all even numbers up to given number?

User Raymond Seger
by
2.1k points

1 Answer

28 votes
28 votes

Answer:

in python:

n = int ( input ( "Pick a number" ) )

total = 0

for num in range ( 0, n+1, 2 ) :

total = total + num

print ( total )

Step-by-step explanation

this algorithm will calculate the sum of all even number from 0 till the number that was inputed by the user. *the reason why there is an n+1 is because in python, the number entered will not be counted. for example if there is an input of 25, this will calculate the sum of even integers from 0 to 24. so we add 1 which will get 26. so if there is an input of 25, the code will solve it from 0 to 25.

User Deivid
by
2.7k points