Tuesday, June 11, 2013

Program to find the sum of the series x-x2/!2+x3/!3-x4/!4+x5/!5-x6/!6

PROGRAM
WAP to find the sum of the series:
                x-x2/!2+x3/!3-x4/!4+x5/!5-x6/!6
-------------------------------------------------------------------------------------------------------------------------------
/**
 * class sumseries2 finds the sum of the series.
 * x-x2/!2+x3/!3-x4/!4+x5/!5-x6/!6
 * @author: Nitendra Verma
 * @version: June 10, 2013
 */
import java.util.Scanner;
public class sumseries2
{
  int num,limit;    //instance variables to store number and limit 
  public sumseries2()  //default constructor to inditilize variables to 0
  {
      num=0;
      limit=0;
    }    
    public void getnum(int n,int l)  //method to assign parameters to varaibles
    {
        num=n;
        limit=l;
    }
    
    public int fact(int n)  //method to find factorial value
    {
        int f=1;
        for(int i=1;i<=n;i++)
        {
            f*=i;
        }     
        return f;           //returning factorial value
    }
    
    public void findsumseries()  //method to find sum of the series 
    {
        float sum=0;
        for(int i=0;i<limit;i++)
        {
            sum+=Math.pow(-1,i)*(Math.pow(num,i+1)/fact(i+1));
        }
        System.out.println("Sum of the series:"+sum);
    }
    
    public static void main()
    {
      sumseries2 ob=new sumseries2();  //creating object of class sumseries2
      Scanner in=new Scanner(System.in); //creating object of class Scanner
      System.out.println("Enetr the number");
      int n=in.nextInt();
      System.out.println("Enetr the limit");
      int l=in.nextInt();
      ob.getnum(n,l);        //calling method
      ob.findsumseries();    //calling method
    }
}

OUTPUT
Enetr the number
2
Enetr the limit
3
Sum of the series:1.3333334



/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in

1 comment:

  1. No use of such a big program it can be done in 10-12 lines only

    ReplyDelete

Ur comments r most welcome...