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

C++Server Side ProgrammingProgramming

In this tutorial, we are going to write a program that finds the n-th number in the series 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
...

Example

Let's see the code.

 Live Demo

#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
Published on 29-Jun-2021 09:35:42
Advertisements