Given a non-negative integer n, return the sum of squares of all integers from 1 to n. The sum is calculated as 11 + 22 + 33 + ... + nn. Return 0 if n is 0 or null.
Examples:
Input: n = 1
Output: 1
Explanation: 1*1 = 1
Input: n = 3
Output: 14
Explanation: 1*1 + 2*2 + 3*3 = 1 + 4 + 9 = 14
Input: n = 5
Output: 55
Explanation: 1*1 + 2*2 + 3*3 + 4*4 + 5*5 = 1 + 4 + 9 + 16 + 25 = 55
Input: n = 0
Output: 0
Explanation: No numbers to square, return 0
Input: n = null
Output: 0
Explanation: Null input returns 0
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.