Find Sum of Series 1 ^ 2 | PHP program to find sum of series 1 ^ 2 - 2 ^ 2 + 3 ^ 3 - 4 ^ 4 + ... Function to find sum of series ; If i is even ; If i is odd ; return the result ; Get n ; Find the sum ; Get n ; Find the sum ; This Code is Contributed by anuj_67
< ? php function sum_of_series ( $ n ) { $ result = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ result = $ result - pow ( $ i , 2 ) ; else $ result = $ result + pow ( $ i , 2 ) ; } return $ result ; } $ n = 3 ; echo sum_of_series ( $ n ) , " STRNEWLINE " ; $ n = 10 ; echo sum_of_series ( $ n ) , " STRNEWLINE " ; ? >