write a program to calculate the sum of 1/2-1/3+1/4-1/5.....n terms
Anonymous:
I'm giving the program in JAVA. If you do have any problem,then inform me. Mention the language if you don't want JAVA.
Answers
Answered by
0
import java.io.*;
class JAVA
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your limit for the series- ");
int n=Integer.parseInt(br.readLine());
int r=0,d=0,S=0,k;
for(int i=1,k=2;i<=n;++i,++k)
{
if(k%2==0)
{
r=r+(1/k);
}
if(k%2!=0)
{
d=d-(1/k);
}
S=S+(r+d);
}
System.out.println("Sum of the series= "+S);
}
}
class JAVA
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your limit for the series- ");
int n=Integer.parseInt(br.readLine());
int r=0,d=0,S=0,k;
for(int i=1,k=2;i<=n;++i,++k)
{
if(k%2==0)
{
r=r+(1/k);
}
if(k%2!=0)
{
d=d-(1/k);
}
S=S+(r+d);
}
System.out.println("Sum of the series= "+S);
}
}
Similar questions