Program to find sum of series 1/2-2/3+3/4-4/5+5/6...+N/N+1

Problem statement:- Program to find the sum of series 1/2-2/3+3/4-4/5+5/6...+N/N+1

 Data requirement:-

   Input Data:- n

  Output Data:-Sum

  Additional Data:-i

Program in C
  
Here is the source code of the C Program to find the sum of series 1/2-2/3+3/4-4/5+5/6 ...+N/N+1.

Code:

#include<stdio.h>
int main()
{
    int n;
    double sum=0.0,i;
    printf("Enter the range of number:");
    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
        if((int)i%2==0)
            sum-=i/(i+1);
        else
            sum+=i/(i+1);
    }
    printf("The sum of the series = %0.2lf",sum);
}

Input/Output:
Enter the range of number:3
The sum of the series = 0.58

Program in C++
  
Here is the source code of the C++ Program to find the sum of series 1/2-2/3+3/4-4/5+5/6 ...+N/N+1.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n;
    double sum=0.0,i;
    cout<<"Enter the range of number:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
      if((int)i%2==0)
            sum-=i/(i+1);
        else
            sum+=i/(i+1);
    }
    cout<<"The sum of the series = "<<sum;
}

Input/Output:
Enter the range of number:5
The sum of the series = 0.616667

Program in Java
  
Here is the source code of the Java Program to find the sum of series 1/2-2/3+3/4-4/5+5/6 ...+N/N+1.

Code:

import java.util.Scanner;
public class p21 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
    int n;
    double sum=0.0,i;
    System.out.println("Enter the range of number:");
    n=cs.nextInt();
    for(i=1;i<=n;i++)
    {
    if((int)i%2==0)
            sum-=i/(i+1);
        else
            sum+=i/(i+1);
    }
    System.out.println("The sum of the series = "+sum);
    cs.close();
}
}

Input/Output:
Enter the range of number:
9
The sum of the series = 0.6456349206349208

Program in Python
  
Here is the source code of the Python Program to find the sum of series 1/2-2/3+3/4-4/5+5/6 ...+N/N+1.

Code:

print("Enter the range of number(Limit):")
n=int(input())
i=1
sum=0.0
while(i<=n):
    if(i%2==0):
        sum-=i/(i+1)
    else:
        sum+=i/(i+1)
    i+=1
print("The sum of the series = ",sum)

Input/Output:
Enter the range of number(Limit):
21
The sum of the series =  0.6709359053399303



Post a Comment

0 Comments