Series 04

Given a number of terms the task is to calculate the value upto ‘n’ terms of the below given series.

Series: x1 + x2 + x3 + x4 + …………… + ‘n’ terms. Where ‘x’ is an number from user.

Java
import java.util.*;
public class Series
{
    public static void main(String args[])
    {
        int n=0,x=0;
        long result=0;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number: ");
        x=sc.nextInt();
        System.out.print("Enter number of terms: ");
        n=sc.nextInt();
        for(int i=1;i<=n;i++)
        {
            result=result+(long)Math.pow(x,i);
        }
        System.out.println("Result of the series: "+result);
    }
}
Java