Program to find N-th term of series 7, 21, 49, 91, 147, 217, …… in C++

C++Server Side ProgrammingProgramming

<p>In this problem, we are given a number n that denotes the nth term of the series. Our task is to create a program to find the N-th term of series 7, 21, 49, 91, 147, 217, &hellip;&hellip; in C++.</p><p><strong>Problem Description</strong> - We will find the nth term of the series 7, 21, 49, 91, 147, 217, &hellip; and for that, we will deduce the general term of the series.</p><p><strong>Let&rsquo;s take an example to understand the problem,</strong></p><p><strong>Input</strong> &minus; N = 5</p><p><strong>Output</strong> &minus; 147</p><h2>Solution Approach:</h2><p>Let&rsquo;s deduce the general term of the given series. The series is &minus;</p><pre class="result notranslate">7, 21, 49, 91, 147, 217, &hellip;</pre><p>We can see that 7 is common here,</p><pre class="result notranslate">7 * (1, 3, 7, 13, 21, 31, ...)</pre><p>Here, we can observe that this series is increasing like a square series. So,</p><pre class="result notranslate">Series: 7 * (12 , (22 - 1), (33 - 2), (42 - 3), (52 - 4), (62 - 5), ....)</pre><p>The general term of the series can be generalized as &minus;</p><pre class="result notranslate">Tn = 7*(n2 - (n-1))</pre><p>Using the general term formula, we can find any value of the series.</p><p><strong>For example,</strong></p><pre class="result notranslate">T<sub>4</sub> = 7*((4^2) - (4-1)) = 7(16 - 3) = 91 T<sub>7</sub> = 7*((7^2) - (7-1)) = 7(49 - 6) = 301</pre><h2>Example</h2><p><a class="demo" href="http://tpcg.io/WJbX9C0k" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;iostream&gt; using namespace std; int findNTerm(int N) { &nbsp; &nbsp;int nthTerm = ( 7*((N*N) - (N - 1)) ); &nbsp; &nbsp;return nthTerm; } int main() { &nbsp; &nbsp;int N = 9; &nbsp; &nbsp;cout&lt;&lt;N&lt;&lt;&quot;th term of the series is &quot;&lt;&lt;findNTerm(N); &nbsp; &nbsp;return 0; }</pre><h2>Output:</h2><pre class="result notranslate">9th term of the series is 511</pre>
raja
Updated on 01-Oct-2020 11:35:06

Advertisements