4-7 Series

posted Feb 12, 2014, 1:54 PM by Dlovan ismail   [ updated Feb 12, 2014, 1:56 PM by Karwan Jacksi ]
/*Write a program to calculate the sum of the following series as shown below
Sum=1+x/2+x^2/3+x^3/4+⋯+x^n/(n+1)
*/

package series;
import java.util.Scanner;//Needed for class Scanner

public class Series 
{
    public static void main(String[] args)
    {

           // Create a Scanner object for keyboard input.
            Scanner input=new Scanner(System.in);

            System.out.print("Enter the value of n : ");
            double n=input.nextDouble();

            System.out.print("Enter the value of x : ");
            double x=input.nextDouble(),sum=0;

            while(n>=0)
            {
                sum+=Math.pow(x,n)/(n+1);
                n--;
            }

            System.out.println("Sum of the series = "+sum);

    }   
}

ċ
Series.java
(1k)
Karwan Jacksi,
Feb 12, 2014, 1:54 PM
Comments