184k views
2 votes
Write an algorithm to find the sum of all even numbers up to given number?

User Krimo
by
3.9k points

1 Answer

4 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 Navyblue
by
4.6k points