n-th term of series 1, 17, 98, 354…… in C++

C++Server Side ProgrammingProgramming

The given series is 1, 17, 98, 354...

If you clearly observe the series, you will find that the n-th number is equal to the 4 powers.

Let's see the pattern.

1 = 1 ^ 4
17 = 1 ^ 4 + 2 ^ 4
98 = 1 ^ 4 + 2 ^ 4 + 3 ^ 4
354 = 1 ^ 4 + 2 ^ 4 + 3 ^ 4 + 4 ^ 4
...

Algorithm

  • Initialise the number N.
  • Initialise the result to 0.
  • Write a loop that iterates from 1 to n.
    • Add 4th power current number to the result.
  • Print the result.

Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h>

using namespace std;

int getNthTerm(int n) {
   int nthTerm = 0;
   for (int i = 1; i <= n; i++) {
      nthTerm += i * i * i * i;
   }
return nthTerm;
}
int main() {
   int n = 7;
   cout << getNthTerm(n) << endl;
   return 0;
}

Output

If you run the above code, then you will get the following result.

4676
raja
Updated on 22-Oct-2021 09:53:36

Advertisements