- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to find n-th term in the series 7, 15, 32, …
In this problem, we are given an integer N. The task is to find n-th term in series 7, 15, 32....
Let’s take an example to understand the problem,
Input
N = 6
Output
281
Explanation
The series upto nth term is 7, 15, 32, 67, 138, 281
Solution Approach
The solution to the problem lies in decoding the series. You can see the series is a mix of series.
The subtracting the values,
T(2) - T(1) = 15 - 7 = 8 T(3) - T(2) = 32 - 15 = 17 So, T(2) = 2*T(1) + 1 T(3) = 2*T(2) + 2 T(n) = 2*T(n-1) + (n-1)
So, the value of the nth term is found using the last term. To find these we will loop from 1 to n and find each value of the series.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int findNthTerm(int n) { if (n == 1) return 7; int termN = 7; for (int i = 2; i <= n; i++) termN = 2*termN + (i - 1); return termN; } int main(){ int n = 12; cout<<"The series is 7, 15, 32, 67...\n"; cout<<n<<"th term of the series is "<<findNthTerm(n); return 0; }
Output
The series is 7, 15, 32, 67... 12th term of the series is 18419
Advertisements