# 1 + (2 + 3 * 4) + (5 + 6 * 7 + 8 * 9 * 10) +....+ nth term

Jan 9th, 2017
173
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. // Problem: Get the sum of 1 + (2 + 3 * 4) + (5 + 6 * 7 + 8 * 9 * 10) + .... + nth term
2.
3. /*
4.  * while n = 10, sum is 782
5.  * while n = 6, sum is 26
6.  * while n = 19, sum is 10123
7.  * while n = 2, sum is 3
8.  */
9.
10. #include <iostream>
11. using namespace std;
12.
13. int main() {
14.     int n; cout << "Input: "; cin >> n; // getting nth term
15.     int sum = 0, loop = 1 /*for first inner loop*/, num = 1 /*number initializtion*/;
16.     bool run = true; // to run the outer loop
17.
18.     while (run) {
19.         int inner_sum = 0; // for taking the value of each closed bracket
20.         for (int x = 1; x <= loop; x++) /*inner loop for the sum of bracket*/ {
21.             int mult = 1;
22.             for (int y = 1; y <= x; y++) /*inner loop for product*/ {
23.                 mult *= num;
24.                 if (num == n) { // if num crosses n then stop multiplying
25.                     run = false;
26.                     break;
27.                 }
28.                 num++; // number increment by 1
29.             }
30.             inner_sum += mult; // sum of each product of a bracket
31.             if (num == n) // if num crosses n then stop next loop
32.                 break;
33.         }
34.         sum += inner_sum; // sum of participated brackets
35.         loop++; // increment of repeatation of inner loop
36.     }
37.     cout << "\nSum: " << sum << endl;
38.     return 0;
39. }
RAW Paste Data