Program to find the sum of the series 1 + x + x ^ 2 + x ^ 3 + . . + x ^ n | Java implementation to find the sum of series 1 + x ^ 2 + x ^ 3 + ... . + x ^ n ; Java code to print the sum of the given series ; First Term ; Loop to print the N terms of the series and compute their sum ; Driver Code
class GFG { static double sum ( int x , int n ) { double i , total = 1.0 , multi = x ; System . out . print ( "1 ▁ " ) ; for ( i = 1 ; i < n ; i ++ ) { total = total + multi ; System . out . print ( multi ) ; System . out . print ( " ▁ " ) ; multi = multi * x ; } System . out . println ( ) ; return total ; } public static void main ( String [ ] args ) { int x = 2 ; int n = 5 ; System . out . printf ( " % .2f " , sum ( x , n ) ) ; } }