In: Computer Science
Fill in the following function to sum the series of 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n using Java Program
Please find your solution below and if any doubt or need change comment.and do upvote .
CODE:
import java.util.Scanner;
public class Main
{
    //rturns the sum of the series
    public static double sumTheSeries(int x,int n){
        double i,sum=1.0;
        double product=x;
        //find sum till n 
        for( i=1;i<=n;i++){
            sum=sum+product/i;
            product=product*x;
        }
        return sum;
    }
        public static void main(String[] args) {
          Scanner sc=new Scanner(System.in);
          //take user input
          System.out.print("Enter value of x: ");
          int x=sc.nextInt();
          System.out.print("Enter value of n: ");
          int n=sc.nextInt();
          System.out.printf("The summation is: %.2f", sumTheSeries(x,n));
        }
}
OUTPUT:
