Program to generates and find thw sum of the series, 1/2 – 2/3 + 3/4 – 4/5 +………

import java.io.*;
class test
{ public static void main(String[] asd)throws IOException
{
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
int n;
double sum=0.0;
String num;
System.out.print("Enter a number:: ");
num=obj.readLine();
n=Integer.parseInt(num);
for(double i=1;i<=n;i++)
{
if(i%2==0)
{
sum=sum-(i/(i+1));
System.out.print(-(i/(i+1))+"  ");
}
else
{
System.out.print((i/(i+1))+"  ");
sum=sum+(i/(i+1));
}
}
System.out.println("Sum of the series="+sum+".");
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s