Sum of Squares of First N Natural Numbers Problem Statement

You are tasked with finding the sum of squares of the first N natural numbers for given test cases.

Input:

The first line contains an integer 'T', the number of test cases. Each of the next 'T' lines contains a single integer 'N', representing the number for which the sum of squares is to be calculated.

Output:

For each test case, output the sum of squares of the first 'N' natural numbers on a new line.

Example:

Input:
2
4
3
Output:
30
14
Explanation:

For N = 4, the sum is 1^2 + 2^2 + 3^2 + 4^2 = 30. For N = 3, the sum is 1^2 + 2^2 + 3^2 = 14.

Constraints:

  • 1 ≤ T ≤ 10^4
  • 1 ≤ N ≤ 5*10^5
  • Time Limit: 1 sec.

Note:

You are not required to print the expected output, it has already been taken care of. Just implement the function.
AnswerBot
1y

The task is to find the sum of squares of the first 'N' natural numbers.

  • Iterate from 1 to N and calculate the square of each number

  • Add all the squares together to get the final sum

  • Return the sum as th...read more

Help your peers!
Select
Add answer anonymously...

Top Business Analyst Interview Questions Asked at Paytm

Q. Explain the different types of SQL joins.
Q. Explain how you would use the ROW_NUMBER window function.
Q. What are the different types of joins in SQL?
Business Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits