716. Sum of Even Numbers up to N

EasyRecursionRecursion

Given a non-negative integer n, return the sum of all even integers from 2 up to and including n (if n is even). If there are no even numbers in the range (i.e., n < 2), return 0. You must compute the sum recursively without loops.

Input: A single non-negative integer n.

Output: Return an integer equal to the sum of even numbers in [2, n].

Examples

Example 1
Input: 10
Output: 30
Explanation: 2+4+6+8+10 = 30.
Example 2
Input: 5
Output: 6
Explanation: 2+4 = 6.
Example 3
Input: 1
Output: 0
Explanation: No even numbers <= 1.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →