Sum of squares of first n natural numbers | Java program to calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ... ; Function to calculate sum ; Driver code
import java . util . * ; import java . lang . * ; class GFG { public static int summation ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . println ( summation ( n ) ) ; } }