You can use the arithmetic formula for 1^2+2^2+3^2+...+n^2 = (n(n+1)(2n+1))/6
You can refer the same program what madakapoor10 was posted but the logic would be different.
Instead for loop you can assign an integer variable and substitute the formula.
Ex: int s = (n(n+1)(2n+1))/6 where n is the input value.
Here you can reduce the looping process and save time