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
4mo
Calculate the sum of squares of the first N natural numbers for given test cases.
Read the number of test cases 'T'
For each test case, read the value of 'N'
Calculate the sum of squares of the first 'N'...read more
girish jagtap
10mo
int in=4;
int op=IntStream.rangeClosed(1, in).map(x->x*x).sum();
System.out.println(op);
Help your peers!
Add answer anonymously...
LTIMindtree Software Developer interview questions & answers
A Software Developer was asked 1mo agoQ. What is a Common Table Expression (CTE) in SQL?
A Software Developer was asked 2mo agoQ. Given a string, how would you count the occurrences of each character?
A Software Developer was asked 6mo agoQ. Write the project code
Popular interview questions of Software Developer
A Software Developer was asked 1mo agoQ1. What is a Common Table Expression (CTE) in SQL?
A Software Developer was asked 2mo agoQ2. Given a string, how would you count the occurrences of each character?
A Software Developer was asked 2mo agoQ3. How do you secure a web API?
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

