Java Programming : Write a JAVA Program of Sum of Series (1+X2+X3+X4+…………).

Posted on:
tags: ,


  Write a JAVA Program of Sum of Series (1+X2+X3+X4+…………).
import java.io.*;
import java.util.*;
public class a12 {
     public static void main(String args[])throws IOException
        {

                 Scanner br = new Scanner (System.in);
                 System.out.println("Enter the no. of terms");
                 int p = br.nextInt();

                System.out.println("Enter value of x");
                 int x = br.nextInt();

double s=0;
for(int i=0;i<p;i++)
{
 s=s+Math.pow(x,i);
}
System.out.println("sum is:"+s);
}
}
OUTPUT:
Enter the no. of terms
4
Enter value of x
2
sum is:15.0

No comments:

Post a Comment

< >