Sum of series formed by difference between product and sum of N natural numbers | C ++ program to implement the above approach ; Function to calculate the sum upto Nth term ; Stores the sum of the series ; Stores the product of natural numbers upto the current term ; Stores the sum of natural numbers upto the upto current term ; Generate the remaining terms and calculate sum ; Update the sum ; Return the sum ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int seriesSum ( int n ) { int sum = 0 ; int currProd = 1 ; int currSum = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { currProd *= i ; currSum += i ; sum += currProd - currSum ; } return sum ; } int main ( ) { int N = 5 ; cout << seriesSum ( N ) << " ▁ " ; }