← all problems

sumOfEvens 🌶️

Write a method sumOfEvens that takes an int n and returns the sum of the even numbers from 2 up to and including n. If n is less than 2 the sum is 0. Use a for loop that steps by 2 (do not test each number with %). For example, sumOfEvens(10) is 2 + 4 + 6 + 8 + 10, which is 30.