- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

#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); }

Term at index 3 is: -2

- Related Questions & Answers
- C++ program to get the Sum of series: 1 – x^2/2! + x^4/4! -…. upto nth term
- C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
- C++ program to Find Nth term of the series 1, 1, 2, 6, 24…
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- C++ program to find nth term of the series 5, 2, 13 41,...
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array 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…
- Construct SLR (1) parsing table for the grammar\n1. E → E + T\n2. E → T\n3. T → T * F\n4. T → F\n5.F → (E)\n6.F → id
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- C++ program to find Nth term of the series 1, 6, 18, 40, 75, ….
- C++ program to find Nth term of the series 0, 2, 4, 8, 12, 18…
- Applying f(x) to each array element in JavaScript

Advertisements