

- 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
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
#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
- Related Questions & Answers
- Program to find nth term in Look and Say Sequence in Python
- Program to find nth term of a sequence which are divisible by a, b, c in Python
- Find nth term of a given recurrence relation in Python
- Find nth term of a given recurrence relation in C++
- Program to find nth Fibonacci term in Python
- Find the Nth term of the series 9, 45, 243,1377…in C++
- C program to find nth term of given recurrence relation
- Find Nth term (A matrix exponentiation example) in C++
- Find the nth term of the series 0, 8, 64, 216, 512,... in C++
- Program to find nth sequence after following the given string sequence rules in Python
- C++ program to find nth term of the series 5, 2, 13 41,...
- Find the Nth term of the series 14, 28, 20, 40,….. using 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…
- C++ program to find Nth term of the series 3, 14, 39, 84…
Advertisements