# Find nth term of the Dragon Curve Sequence in C++

Here we will see a program, that can find nth term of the Dragon Curve sequence. The Dragon curve sequence is an infinite binary sequence. It starts with 1, and in each step, it alternatively adds 1s and 0s before and after each element of the previous term, to form the next term.

• Term 1 : 1
• Term 2 : 110
• Term 3 : 1101100
• Term 4 : 110110011100100

We will start with 1, then add 1 and 0, alternatively after each element of the preceding term. When the new term obtained becomes the current term, then repeat the steps from 1 to n to generate next terms.

## Example

Live Demo

#include <iostream>
using namespace std;
string dragCurveTerm(int n) {
string term = "1";
for (int i = 2; i <= n; i++) {
string temp = "1";
char prev = '1', zero = '0', one = '1';
for (int j = 0; j < term.length(); j++) {
temp += term[j]; //take character from original string
if (prev == '0') {
temp += one;
prev = one;
} else {
temp += zero;
prev = zero;
}
}
term = temp;
}
return term;
}
int main() {
cout << "4th term of Dragon Curve Sequence: " << dragCurveTerm(4);
}

## Output

4th term of Dragon Curve Sequence: 110110011100100