Write a program to compute the sum of the first n terms of the following series:

S = 1 - 1 / (2 ^ 2) + 1 / (3 ^ 3) - ... 1 / (n ^ n)
where ^ is exponentiation.
The number of terms n is to be taken from user through command line. If command line argument is not found then prompt the user to enter the value of n.

Source code:


#include <iostream>
#include <math.h>
using namespace std;

int main ()
{
    int l;
    double sum = 0,a;

    cout << "Enter the length: " << endl;
    cin >> l;
 
    for (int i=1; i<=l; i++)
    {
        if (i%2 == 0)
        {
            a = -1/pow(i,i);
            sum += a;
        }
        else
        {
            a = 1/pow(i,i);
            sum += a;
         
        }
           cout << "1/" << i << "^" << i << " = " << a << endl;
    }
 
    cout << "The sum of the series is: " << sum << endl;

    return 0;
}