- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Find the Nth term of the series where each term f[i] = f[i – 1] – f[i – 2] in C++
Suppose we have a series called f. Each term of f, follows this rule f[i] = f[i – 1] – f[i – 2], we have to find the Nth term of this sequence. f[0] = X and f[1] = Y. If X = 2 and Y = 3, and N = 3. The result will be -2.
If we see this closely, there will be almost six terms before the sequence starts repeating itself. So we will find the first 6 terms of the series and then the Nth term will be the same as (N mod 6)th term.
Example
#include< iostream> using namespace std; int searchNthTerm(int x, int y, int n) { int terms[6]; terms[0] = x; terms[1] = y; for (int i = 2; i < = 5; i++) terms[i] = terms[i - 1] - terms[i - 2]; return terms[n % 6]; } int main() { int x = 2, y = 3, n = 3; cout << "Term at index " < < n << " is: "<< searchNthTerm(x, y, n); }
Output
Term at index 3 is: -2
- Related Articles
- C++ program to Find Nth term of the series 1, 1, 2, 6, 24…
- 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,...
- Find the nth term of the given series 0, 0, 2, 1, 4, 2, 6, 3, 8, 4… in C++
- Find the Nth term of the series 9, 45, 243,1377…in C++
- C++ program to find Nth term of the series 1, 6, 18, 40, 75, ….
- When the object is placed between $f$ and 2$f$ of a convex lens, the image formed is(a) at $f$ (b) at 2$f$ (c) beyond 2$f$ (d) between O and $f$
- C++ program to find Nth term of series 1, 4, 15, 72, 420…
- C++ program to find Nth term of the series 0, 2, 4, 8, 12, 18…
- Find the Nth term of the series 14, 28, 20, 40,….. using C++
- C++ program to get the Sum of series: 1 – x^2/2! + x^4/4! -…. upto nth term
- Find the nth term of the series 0, 8, 64, 216, 512,... in C++
- C++ program to find Nth term of the series 3, 14, 39, 84…
- Construct SLR (1) parsing table for the grammar 1. E → E + T 2. E → T 3. T → T * F 4. T → F 5.F → (E) 6.F → id

Advertisements