C++ Program to Compute Sum of n^2/(n-1) Series


In mathematics, there are many series whose sum is calculated using computer programs to reduces the effort of doing enormous computations. This example program computes the sum of series ( n^2/(n-1)). The user has to input the value of n.

To write such programs you must be careful about two things – accuracy and type conversions. Accuracy means correct to at least 3 decimal places because series involves real values. A small difference can produce wrong results.

Sometimes we need to do arithmetic between an integer value and a float value. In such cases, to get the correct value of the expression to convert both operands into the same type using C++ type cast operator.

Before you begin, learn basics of C++ programming. Continue reading if you are familiar with the basics.

Problem Definition

We want to write a program that computes the sum of the following series and prints an output to the console.


When the program runs, the user provides the value of n and the program computes the sum of all values up to n. The program cannot have a value less than 2 because any value less than 2 is undefined.

For example, n = 1

= 1^2/(1 - 1) = 1/0 = undefined

To understand the problem let us do a manual computation and verify the results using our example program. Suppose n = 5 then

= 2^2/(2 - 1) + 3^2/(3-1) + 4^2/(4-1) + 5^2/(5-1)

= 4/1 + 9/2 + 16/3 + 25/4

You can use a calculator or do fraction addition. The final sum of the series is given below.

= 20.08

Now, you need to verify the output by running the C++ program for the sum of the series given below.

Program Source Code

#include <cstdlib>
#include <iostream>

using namespace std;

int main()

    int n,i;
    double sum;

//initialize sum to 0

    sum = 0.0;

//read value of N

    cout << "Enter the value of n:";
    cin >> n;

//Compute the value of the sum, but the equation has a restriction. 
//The value of n should not be less than 2


    sum = sum + float(i * i)/float(i-1);


//Printing the Output to Console

    cout << "\n";
    cout << "Sum of the Series n^2/(n-1) =" << " " << sum << endl;
    cout << "\n";

    return EXIT_SUCCESS;



The output of the program is the same what we received using manual computation method. Therefore, the program works correctly. You may try different inputs.

Output - Sum of Series
Output – Sum of Series n^2/(n-1)

Related Articles:-