Sum of First N Even Numbers
EasyPractice library · +10 XPdynamic programmingiteration
Given a positive integer N, calculate the sum of the first N positive even numbers. The first positive even number is 2.
Input
A single line containing the integer N.
Output
A single line containing the sum of the first N positive even numbers.
Constraints
1 <= N <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB12 hidden tests
Example 1
Input
3
Output
12
The first 3 positive even numbers are 2, 4, 6. Their sum is 2 + 4 + 6 = 12.
Example 2
Input
5
Output
30
The first 5 positive even numbers are 2, 4, 6, 8, 10. Their sum is 2 + 4 + 6 + 8 + 10 = 30.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
3
Example 2 input
5