
- 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
Program to find N-th term of series 0, 2,1, 3, 1, 5, 2, 7, 3...in C++
In this problem, we are given a number N. Our task is to create a Program to find N-th term of series 0, 2, 1, 3, 1, 5, 2, 7, 3...in C++.
Problem Description − We are given the Series −
0, 2, 1, 3, 1, 5, 2, 7, 3...N Term
To find the Nth term of this series, we will formula the general term of the series and then find the Nth term.
Let’s take an example to understand the problem,
Input − N = 7
Output − 2
Solution Approach :
To solve the problem and find the general formula for the series. We need to closely observe the series as it has two different series inside it. This type of series is a bit confusing at the start but once you know that it’s a mixture series, you will find it easy to find the general term.
Here, there are two series’ one on even index and the other on the odd index. Let’s see them individually.
Even index series: 0, 1, 1, 2, 3, ….
Odd index series: 2, 3, 5, 7, …
Now, it must be clear to you that the even series is the Fibonacci series. And the odd series is a series of prime numbers.
So, the series is −
If N is odd, (N/2) index fibonacci series.
If N is Even, (N/2) index prime number.
Program to illustrate the working of our solution,
#include<iostream> using namespace std; int findNthPrimeTerm(int n) { int primeCount = 0; for (int i = 2; ; i++) { int isPrime = 1; for (int j = 2; j <= (i/2); j++) { if (i % j == 0) { isPrime = 0; break; } } if (isPrime) primeCount++; if (primeCount == n) { return i; break; } } return -1; } int FibonaciiNthTerm(int n) { int nthTerm = 1, last = 0; int i; if( n == 0) return 0; else if( n == 1) return 1; else{ for (i = 2; i <= n; i++) { nthTerm += last; last = nthTerm - last ; } return nthTerm; } } int findNTerm(int N) { if (N % 2 == 0) return findNthPrimeTerm(N/2); else { return FibonaciiNthTerm(N/2); } } int main() { int N = 13; cout<<N<<"th term of the series is "<<findNTerm(N)<<endl; N = 4; cout<<N<<"th term of the series is "<<findNTerm(N); return 0; }
Output:
13th term of the series is 8 4th term of the series is 3
- Related Articles
- Program to find N-th term of series 0, 0, 2, 1, 4, 2, 6, 3, 8…in C++
- C++ Programe to find n-th term in series 1 2 2 3 3 3 4
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- Program to find N-th term of series 1, 3, 12, 60, 360...in C++
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++\n
- Observe the following pattern$1 + 3 = 2^2$$1 + 3 + 5 = 3^2$$1+3 + 5 + 7 = 4^2$and write the value of $1 + 3 + 5 + 7 + 9 +…………$ upto $n$ terms.
- Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
