# Program to find N-th term of series 1, 2, 11, 12, 21… in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

In this problem, we are given a number N. Our task is to create a program to find N-th term of series 1, 2, 11, 12, 21… in C++.

## Problem Description

To find the Nth term of the series −

1, 2, 11, 12, 21, 22, 111, 112, .... Nterms

We will find the general term of the series.

Let’s take an example to understand the problem,

N = 8

112

## Solution Approach

To derive the general term, we need to closely observe the series. In this series, we can see that there are only 1’s and 2’s in the value. And every term is an alternation of 1 and 2. So, the general term will be,

$$T_{(N)}=T_{(n/2)}*10 + 1,\:if\:N\:is\:odd.$$

$$T_{(N)}= T_{((n/2)-1)}*10 + 2, if\:N\:is\:even.$$

## Example

Live Demo

#include <iostream>
using namespace std;
int findNTerm(int N) {
if(N == 1)
return 1;
if(N == 2)
return 2;
int value;
if(N%2 == 0){
value = (findNTerm((N/2)-1)*10) + 2;
}
else
value = (findNTerm((N/2))*10) + 1; return value;
}
int main() {
int N = 12;
cout<<N<<"Th term of the series is "<<findNTerm(N);
return 0;
}

## Output

12Th term of the series is 212
Updated on 09-Oct-2020 07:14:38