
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the Nth term of the series 14, 28, 20, 40,….. using C++
In this problem, we are given an integer value N.Our task is to find the nth term of the series −
14, 28, 20, 40, 32, 64, 56, 112….
Let’s take an example to understand the problem,
Input
N = 6
Output
64
Solution Approach
To find the Nth term of the series we need to find the general term of the series. For which we need to observe the series closely. I can see two different ways to solve the series.
Method 1
The series is a mixture of two different series at even and odd positions.
At odd positions − 14, 20, 32, 56, ….
T1 = 14 T3 = 20 = T1 + 6 T5 = 32 = T3 + 12 T7 = 56 = T5 + 24 = T1 + 6 + 12 + 24 = T1 + 6*(1 + 2 + 4) TN = T1 + 6(20 + 21 + 22 +....+ 2((N/2) - 1 ) )
At even positions − 28, 40, 64, 112…
T2 = 28 T4 = 40 = T2 + 12 T6 = 64 = T4 + 24 T8 = 112 = T6 + 48 = T2 + 12 + 24 + 48 = T2 + 6*(2 + 4 + 8) TN = T2 + 6(21 + 22 +....+ 2((N/2) - 1 ) )
The Nth term of the series is
$\mathrm{T_{N}\, =\, T_{s}\, +\, 6\left ( \sum 2^{\left ( \left ( N/2 \right )-1 \right )} \right )}$ ,where value are from s to N incremented by 2.
For even values s = 2,
For odd values s = 1.
Example
#include <iostream> #include <math.h> using namespace std; long findNthAdd(int s, int i, int n){ int sum = 0; for(; i <= n; i+= 2){ sum += pow(2, (int)((i/2) - 1)); } return 6*sum; } long findNthTermSeries(int n){ int s, i; if(n % 2 == 0){ s = 28; i = 4; } else{ s = 14; i = 3; } return ( s + findNthAdd(s, i, n)); } int main(){ int n = 15; cout<<n<<"th term of the series is "<<findNthTermSeries(n); return 0; }
Output
15th term of the series is 776
Another solution
One more way, using which the Nth term can be found is using the fact that the current term is either twice of the previous term or it 8 less from the previous term based on it’s even/odd.
If N is even TN = 2*T(N-1) If N is odd TN = T(N-1) - 8
So, we need to loop from 2 to N, and find Ti by checking if its even or odd.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; bool isEven(int N){ if(N % 2 == 0) return true; return false; } int findNthTermSeries(int n){ int TermN = 14; for (int i = 2; i <= n; i++) { if (isEven(i)) TermN *= 2; else TermN -= 8; } return TermN; } int main(){ int n = 15; cout<<n<<"th term of the series is "<<findNthTermSeries(n); return 0; }
Output
15th term of the series is 776
- Related Articles
- C++ program to find Nth term of the series 3, 14, 39, 84…
- C++ program to find Nth term of the series 1, 6, 18, 40, 75, ….
- Find the Nth term of the series 9, 45, 243,1377…in C++
- C++ program to find Nth number of the series 1, 6, 15, 28, 45, …..
- Program to find N-th term of series 4, 14, 28, 46, 68, 94, 124, 158, …..in C++
- Find the nth term of the series 0, 8, 64, 216, 512,... in C++
- C++ program to find Nth term of the series 1, 5, 32, 288 …
- C++ program to find Nth term of the series 1, 8, 54, 384…
- C++ program to find nth term of the series 5, 2, 13 41,...
- C++ program to Find Nth term of the series 1, 1, 2, 6, 24…
- C++ program to find Nth term of the series 5, 13, 25, 41, 61…
- Find the sum of first 20 terms of the sequence whose nth term is $a_n = An + B$.
- Find the Nth term of the series where each term f[i] = f[i – 1] – f[i – 2] in C++
- C++ program to find Nth term of the series 0, 2, 4, 8, 12, 18…
- JavaScript code to find nth term of a series - Arithmetic Progression (AP)
