WAP in C++ and Python to find the sum of the series x+ x2/2 +x3/3 + ……. +xn/n

Posted in

WAP in C++ and Python to find the sum of the series x+ x2/2 +x3/3 + ……. +xn/n

Vinay Khatri
Last updated on July 19, 2022

    Sum of the series is a common mathematical series that has several uses, and thus, is important. It has several variations. This article will discuss 2 programs, one in C++ and the other in Python. Both programs aim to calculate for the series x + x 2 /2 +x 3 /3 + ……. +x n /n.

    The Sum of the Series x + x 2 /2 +x 3 /3 + ……. +x n /n Program

    Here in these 2 programs, we will code to calculate the sum of the series x + x 2 /2 +x 3 /3 + ……. +x n /n, where x and n are two user-defined variables.

    First, we will implement the program in C++ and then in the Python programming language.

    C++ Program

    #include<iostream.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
    clrscr();
    long float sum=0,x,n,i;
    cout<<"Here we will calculate the sum of series x + (x^2)/2+ ...+ (x^n)/n \n";
    cout<<"Enter the value of x :";
    cin>>x;
    cout<<"Enter the value of n: ";
    cin>>n;
    
    for(i=1;i<=n;i++)
        {
        sum += pow(x,i)/i;
        }
    cout<<"The sum of the series is: "<<sum;
    getch();
    }

    Output:

    Here we will calculate the sum of series x+x^2/2+…..+x^n/n
    
    Enter the value of x: 4
    Enter the value of n: 5
    The sum of the series is: 302.1333
    
    

    Python Program

    import math
    sum=0
    print("Here we will calculate the sum of series x+x^2/2+…..+x^n/n")
    x=int(input("Enter the value of x: "))
    n = int(input("Enter the value of n: "))
    for i in range(1,n+1):
        sum+=math.pow(x,i)/i
    print("The sum of the series is:", sum)
    Output:
    
    Here we will calculate the sum of series x+x^2/2+…..+x^n/n
    
    Enter the value of x: 4
    Enter the value of n: 3
    The sum of the series is: 33.333
    
    

    Conclusion

    That sums up this article detailing two programs, each in C++ and Python, to find the sum of the series x + x 2 /2 +x 3 /3 + ……. +x n /n. There can be different programs for calculating other variations of the series.

    Once you get the gist of the algorithm used in the programs mentioned above, you can implement the same program in other languages too, may it be C, Java, or the R programing language.

    Hope you understand the programs well and now are able to implement the logic on your own. If you have any queries regarding the same, you can drop them in the comments section below. We will try our best to resolve them.

    People are also reading:

    Leave a Comment on this Post

    0 Comments