Program to find sum of series 1+1/3+1/5+1/7+.....1/(N+2)

Problem statement:- Program to find the sum of series 1+1/3+1/5+1/7+.....1/(N+2)

 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+1/3+1/5+1/7+ ..... 1/(N+2).

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+=2)
    {
        sum+=1/i;
    }

    printf("The sum of the series = %0.2lf",sum);
}

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

Program in C++
  
Here is the source code of the C++ Program to find the sum of series 1+1/3+1/5+1/7+ ..... 1/(N+2).

Code:

#include<iostream>
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+=2)
    {
        sum+=1/i;
    }
    cout<<"The sum of the series = "<<sum;
}

Input/Output:
Enter the range of number:7
The sum of the series = 1.67619

Program in Java
  
Here is the source code of the Java Program to find the sum of series 1+1/3+1/5+1/7+ ..... 1/(N+2).

Code:

import java.util.Scanner;
public class p29 {

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+=2)
    {
    sum+=1/i;
    }
    System.out.println("The sum of the series = "+sum);
    cs.close();
}
}

Input/Output:
Enter the range of number:
13
The sum of the series = 1.9551337551337549

Program in Python
  
Here is the source code of the Python Program to find the sum of series 1+1/3+1/5+1/7+ ..... 1/(N+2).

Code:

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

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


Post a Comment

0 Comments