Write a Program to find the Sum of Series 1/2+4/5+7/8+…

Posted in

Write a Program to find the Sum of Series 1/2+4/5+7/8+…

Vinay Khatri
Last updated on June 9, 2022

    Problem Statement

    In this programming tutorial, we need to write a script or program that can compute the sum of the following series 1/2+4/5+7/8+……(1+(3n-3)/2+(2n-2)) . The series goes in the following pattern

    • The difference between the two consecutive numerators and denominators values is 3.
    • And the series starts with 1/2

    The end values of the series will be

    1+(3n-3) / 2+(3n-3)

    Where n represent the total number of elements present in the series. For example Let's say if the value of n is 3 Input

    n=3

    Output

    2.175

    Explanation If the value of n is 3, this means only 3 elements will be present in the series.

    =1/2 + 4/5 + 7/8
    =0.5+0.8+0.875
    =2.17

    Solution

    To solve the problem we will use the brute force approach in which we will take the help of for loops, and keep adding the series values into the total sum. We will create a loop from range 1 to 1+(3n-3) , and with a 3 step iteration jump, here n is the number of elements or values present in the series.

    C Program to find the Sum of Series 1/2+4/5+7/8 +...

    #include <stdio.h>
    int main()
    {	
    	float sum=0,n,i, numerator, denominator;
    	
    	printf("Here we will calculate the sum of series 1/2+4/5+7/8 + ...  \n");
    	//input the value of n
    	printf("Enter the value of n: ");
    	scanf("%f", &n);
    	
    	//compute the sum
    	
    	for(i=1;i<=(1+(3*n-3)); i+=3)
    	{
    		numerator = i;
    		denominator = i+1;
    		
    		//compute the sum		
    		sum += numerator / denominator;
    	}
    	
    	printf("The sum of the series is: %.2f",sum);
    
    	return 0;
    }

    Output

    Here we will calculate the sum of series 1/2+4/5+7/8 + ...á
    Enter the value of n: 3
    The sum of the series is: 2.17

    C++ program to find the sum of series 1/2+4/5+7/8+…

    #include<iostream>
    using namespace std;
    int main()
    {
    	float sum=0,n, numerator, denominator;
    	cout<<"Here we will calculate the sum of series 1/2+4/5+7/8+… \n";
    	
    	//input the value of n
    	cout<<"Enter the value of n: "; cin>>n;
    	
    	for(int i=1;i<=(1+(3*n-3)); i+=3)
    	{
    		numerator = i;
    		denominator = i+1;
    		
    		//compute the sum		
    		sum += numerator / denominator;
    	}
    	
    	cout<<"The sum of the series is: "<<sum;
    	return 0; 
    }

    Output

    Here we will calculate the sum of series 1/2+4/5+7/8+...
    Enter the value of n: 4
    The sum of the series is: 3.08409

    Python Program to find the Sum of Series 1/2+4/5+7/8+…

    print("Here we will calculate the sum of series 1/2+4/5+7/8+… ")
    
    # initialize the value of sum
    sum_=0
    
    # input the value of n
    n = int(input("Enter the value of n: "))
    
    for i in range(1, (1+(3*n-3))+1, 3):
        numerator =i
        denominator = i+1
    
        # compute the sum
        sum_+=numerator/denominator
    
    print(f"The sum of the series is: {round(sum_,3)}")
    

    Output:

    Here we will calculate the sum of series 1/2+4/5+7/8+…
    Enter the value of n: 4
    The sum of the series is: 3.084

    Wrapping Up!

    In this tutorial, we discussed how to print the sum of a series 1/2+4/5+7/8+… using C, C++ and Python programming language. To find the sum of the series we used the brute force technique in which we used a for loop and add every number of the series to the total sum. If you like this article or have any suggestions please let us know by commenting down below. People are also reading:

    Leave a Comment on this Post

    0 Comments