Thursday, November 19, 2015

Print a series of values divided by its factorial values


Show the sum of the series: 1-2/2!+3/3!-4/4! + ...+ n/n!



import java.io.*;
class Num
{
int i,n;
double sum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 public void take () throws Exception
 {
    System.out.println("Take the value of 'n':");
    n=Integer.parseInt(br.readLine());
    for( i=1;i<=n;i++)
    {
   if(i%2==0)
   sum=sum-(double)i/fact(i);
   else
   sum=sum+(double)i/fact(i);
   }
    System.out.println("Sum of the series="+sum);
    }

    private int fact(int x)
    {
     int f=1;
     for(int j=2;j<=x;j++)
     f=f*j;
     return f;
     }
   public static void main(String args[]) throws Exception
   {
    Num object=new Num();
    object.take();
    }
    }

Back To BlueJ Series Display Programs Home Page: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner