# Generate palindromic binary strings of given length N

Here given code implementation process.

// C Program
// Generate palindromic binary strings of given length N
#include <stdio.h>

// Display calculated result
void printResult(char result[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%c ", result[i]);
}
printf("\n ");
}
void sequence(char result[], int index, int n)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of odd length palindrome sequence
result[index] = '0';
printResult(result, n);
result[index] = '1';
printResult(result, n);
}
else
{
printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
// Set the value of boundary element is to zero
result[index] = '0';
result[(n - 1) - index] = '0';
sequence(result, index + 1, n);
// Set the value of boundary element is to one
result[index] = '1';
result[(n - 1) - index] = '1';
sequence(result, index + 1, n);
}
}
void palindrome(int n)
{
if (n <= 0)
{
return;
}
printf("\n Given Length : %d\n ", n);
char result[n];
sequence(result, 0, n);
}
int main()
{
int n = 6;
/*
Test A
n = 6
----------------
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1
-----------
*/
palindrome(n);
n = 5;
/*
Test B
n = 5
-------------
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
------------
*/
palindrome(n);
return 0;
}

#### Output

Given Length : 6
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1

Given Length : 5
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
// Java program for
// Generate palindromic binary strings of given length N
public class Combination
{
// Display calculated result
public void printResult(char[] result, int n)
{
for (int i = 0; i < n; i++)
{
System.out.print("  " + result[i]);
}
System.out.print("\n ");
}
public void sequence(char[] result, int index, int n)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
result[index] = '0';
printResult(result, n);
result[index] = '1';
printResult(result, n);
}
else
{
printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
// Set the value of boundary element is to zero
result[index] = '0';
result[(n - 1) - index] = '0';
sequence(result, index + 1, n);
// Set the value of boundary element is to one
result[index] = '1';
result[(n - 1) - index] = '1';
sequence(result, index + 1, n);
}
}
public void palindrome(int n)
{
if (n <= 0)
{
return;
}
System.out.print("\n Given Length : " + n + "\n ");
char[] result = new char[n];
sequence(result, 0, n);
}
public static void main(String[] args)
{
int n = 6;
/*
Test A
n = 6
----------------
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1
-----------
*/
n = 5;
/*
Test B
n = 5
-------------
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
------------
*/
}
}

#### Output

Given Length : 6
0  0  0  0  0  0
0  0  1  1  0  0
0  1  0  0  1  0
0  1  1  1  1  0
1  0  0  0  0  1
1  0  1  1  0  1
1  1  0  0  1  1
1  1  1  1  1  1

Given Length : 5
0  0  0  0  0
0  0  1  0  0
0  1  0  1  0
0  1  1  1  0
1  0  0  0  1
1  0  1  0  1
1  1  0  1  1
1  1  1  1  1
#include <iostream>

using namespace std;
// C++ program for
// Generate palindromic binary strings of given length N
class Combination
{
public:
// Display calculated result
void printResult(char result[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "  " << result[i];
}
cout << "\n ";
}
void sequence(char result[], int index, int n)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
result[index] = '0';
this->printResult(result, n);
result[index] = '1';
this->printResult(result, n);
}
else
{
this->printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
// Set the value of boundary element is to zero
result[index] = '0';
result[(n - 1) - index] = '0';
this->sequence(result, index + 1, n);
// Set the value of boundary element is to one
result[index] = '1';
result[(n - 1) - index] = '1';
this->sequence(result, index + 1, n);
}
}
void palindrome(int n)
{
if (n <= 0)
{
return;
}
cout << "\n Given Length : "
<< n << "\n ";
char result[n];
this->sequence(result, 0, n);
}
};
int main()
{
int n = 6;
/*
Test A
n = 6
----------------
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1
-----------
*/
n = 5;
/*
Test B
n = 5
-------------
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
------------
*/
return 0;
}

#### Output

Given Length : 6
0  0  0  0  0  0
0  0  1  1  0  0
0  1  0  0  1  0
0  1  1  1  1  0
1  0  0  0  0  1
1  0  1  1  0  1
1  1  0  0  1  1
1  1  1  1  1  1

Given Length : 5
0  0  0  0  0
0  0  1  0  0
0  1  0  1  0
0  1  1  1  0
1  0  0  0  1
1  0  1  0  1
1  1  0  1  1
1  1  1  1  1
// Include namespace system
using System;
// Csharp program for
// Generate palindromic binary strings of given length N
public class Combination
{
// Display calculated result
public void printResult(char[] result, int n)
{
for (int i = 0; i < n; i++)
{
Console.Write("  " + result[i]);
}
Console.Write("\n ");
}
public void sequence(char[] result, int index, int n)
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
result[index] = '0';
this.printResult(result, n);
result[index] = '1';
this.printResult(result, n);
}
else
{
this.printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
// Set the value of boundary element is to zero
result[index] = '0';
result[(n - 1) - index] = '0';
this.sequence(result, index + 1, n);
// Set the value of boundary element is to one
result[index] = '1';
result[(n - 1) - index] = '1';
this.sequence(result, index + 1, n);
}
}
public void palindrome(int n)
{
if (n <= 0)
{
return;
}
Console.Write("\n Given Length : " + n + "\n ");
char[] result = new char[n];
this.sequence(result, 0, n);
}
public static void Main(String[] args)
{
int n = 6;
/*
Test A
n = 6
----------------
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1
-----------
*/
n = 5;
/*
Test B
n = 5
-------------
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
------------
*/
}
}

#### Output

Given Length : 6
0  0  0  0  0  0
0  0  1  1  0  0
0  1  0  0  1  0
0  1  1  1  1  0
1  0  0  0  0  1
1  0  1  1  0  1
1  1  0  0  1  1
1  1  1  1  1  1

Given Length : 5
0  0  0  0  0
0  0  1  0  0
0  1  0  1  0
0  1  1  1  0
1  0  0  0  1
1  0  1  0  1
1  1  0  1  1
1  1  1  1  1
package main
import "fmt"
// Go program for
// Generate palindromic binary strings of given length N
type Combination struct {}
func getCombination() * Combination {
var me *Combination = &Combination {}
return me
}
// Display calculated result
func(this Combination) printResult(result[] byte,
n int) {
for i := 0 ; i < n ; i++ {
fmt.Print("  ", string(result[i]))
}
fmt.Print("\n ")
}
func(this Combination) sequence(result[] byte,
index int, n int) {
if index == n / 2 {
if (n % 2) != 0 {
// Handles the request of
// odd length palindrome sequence
result[index] = '0'
this.printResult(result, n)
result[index] = '1'
this.printResult(result, n)
} else {
this.printResult(result, n)
}
} else if index > n / 2 {
return
} else {
// Set the value of boundary element is to zero
result[index] = '0'
result[(n - 1) - index] = '0'
this.sequence(result, index + 1, n)
// Set the value of boundary element is to one
result[index] = '1'
result[(n - 1) - index] = '1'
this.sequence(result, index + 1, n)
}
}
func(this Combination) palindrome(n int) {
if n <= 0 {
return
}
fmt.Print("\n Given Length : ", n, "\n ")
var result = make([] byte, n)
this.sequence(result, 0, n)
}
func main() {
var task * Combination = getCombination()
var n int = 6
/*
Test A
n = 6
----------------
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1
-----------
*/
n = 5
/*
Test B
n = 5
-------------
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
------------
*/
}

#### Output

Given Length : 6
0  0  0  0  0  0
0  0  1  1  0  0
0  1  0  0  1  0
0  1  1  1  1  0
1  0  0  0  0  1
1  0  1  1  0  1
1  1  0  0  1  1
1  1  1  1  1  1

Given Length : 5
0  0  0  0  0
0  0  1  0  0
0  1  0  1  0
0  1  1  1  0
1  0  0  0  1
1  0  1  0  1
1  1  0  1  1
1  1  1  1  1
<?php
// Php program for
// Generate palindromic binary strings of given length N
class Combination
{
// Display calculated result
public	function printResult(\$result, \$n)
{
for (\$i = 0; \$i < \$n; \$i++)
{
echo("  ".\$result[\$i]);
}
echo("\n ");
}
public	function sequence(\$result, \$index, \$n)
{
if (\$index == (int)(\$n / 2))
{
if ((\$n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
\$result[\$index] = '0';
\$this->printResult(\$result, \$n);
\$result[\$index] = '1';
\$this->printResult(\$result, \$n);
}
else
{
\$this->printResult(\$result, \$n);
}
}
else if (\$index > (int)(\$n / 2))
{
return;
}
else
{
// Set the value of boundary element is to zero
\$result[\$index] = '0';
\$result[(\$n - 1) - \$index] = '0';
\$this->sequence(\$result, \$index + 1, \$n);
// Set the value of boundary element is to one
\$result[\$index] = '1';
\$result[(\$n - 1) - \$index] = '1';
\$this->sequence(\$result, \$index + 1, \$n);
}
}
public	function palindrome(\$n)
{
if (\$n <= 0)
{
return;
}
echo("\n Given Length : ".\$n.
"\n ");
\$result = array_fill(0, \$n, ' ');
\$this->sequence(\$result, 0, \$n);
}
}

function main()
{
\$n = 6;
/*
Test A
n = 6
----------------
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1
-----------
*/
\$n = 5;
/*
Test B
n = 5
-------------
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
------------
*/
}
main();

#### Output

Given Length : 6
0  0  0  0  0  0
0  0  1  1  0  0
0  1  0  0  1  0
0  1  1  1  1  0
1  0  0  0  0  1
1  0  1  1  0  1
1  1  0  0  1  1
1  1  1  1  1  1

Given Length : 5
0  0  0  0  0
0  0  1  0  0
0  1  0  1  0
0  1  1  1  0
1  0  0  0  1
1  0  1  0  1
1  1  0  1  1
1  1  1  1  1
// Node JS program for
// Generate palindromic binary strings of given length N
class Combination
{
// Display calculated result
printResult(result, n)
{
for (var i = 0; i < n; i++)
{
process.stdout.write("  " + result[i]);
}
process.stdout.write("\n ");
}
sequence(result, index, n)
{
if (index == parseInt(n / 2))
{
if ((n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
result[index] = '0';
this.printResult(result, n);
result[index] = '1';
this.printResult(result, n);
}
else
{
this.printResult(result, n);
}
}
else if (index > parseInt(n / 2))
{
return;
}
else
{
// Set the value of boundary element is to zero
result[index] = '0';
result[(n - 1) - index] = '0';
this.sequence(result, index + 1, n);
// Set the value of boundary element is to one
result[index] = '1';
result[(n - 1) - index] = '1';
this.sequence(result, index + 1, n);
}
}
palindrome(n)
{
if (n <= 0)
{
return;
}
process.stdout.write("\n Given Length : " +
n + "\n ");
var result = Array(n).fill(' ');
this.sequence(result, 0, n);
}
}

function main()
{
var n = 6;
/*
Test A
n = 6
----------------
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1
-----------
*/
n = 5;
/*
Test B
n = 5
-------------
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
------------
*/
}
main();

#### Output

Given Length : 6
0  0  0  0  0  0
0  0  1  1  0  0
0  1  0  0  1  0
0  1  1  1  1  0
1  0  0  0  0  1
1  0  1  1  0  1
1  1  0  0  1  1
1  1  1  1  1  1

Given Length : 5
0  0  0  0  0
0  0  1  0  0
0  1  0  1  0
0  1  1  1  0
1  0  0  0  1
1  0  1  0  1
1  1  0  1  1
1  1  1  1  1
#  Python 3 program for
#  Generate palindromic binary strings of given length N
class Combination :
#  Display calculated result
def printResult(self, result, n) :
i = 0
while (i < n) :
print("  ", result[i], end = "")
i += 1

print("\n ", end = "")

def sequence(self, result, index, n) :
if (index == int(n / 2)) :
if ((n % 2) != 0) :
#  Handles the request of
#  odd length palindrome sequence
result[index] = '0'
self.printResult(result, n)
result[index] = '1'
self.printResult(result, n)
else :
self.printResult(result, n)

elif (index > int(n / 2)) :
return
else :
#  Set the value of boundary element is to zero
result[index] = '0'
result[(n - 1) - index] = '0'
self.sequence(result, index + 1, n)
#  Set the value of boundary element is to one
result[index] = '1'
result[(n - 1) - index] = '1'
self.sequence(result, index + 1, n)

def palindrome(self, n) :
if (n <= 0) :
return

print("\n Given Length : ", n ,"\n ", end = "")
result = [ ' ' ] * (n)
self.sequence(result, 0, n)

def main() :
n = 6
#    Test A
#    n = 6
#    ----------------
#    0 0 0 0 0 0
#    0 0 1 1 0 0
#    0 1 0 0 1 0
#    0 1 1 1 1 0
#    1 0 0 0 0 1
#    1 0 1 1 0 1
#    1 1 0 0 1 1
#    1 1 1 1 1 1
#    -----------
n = 5
#    Test B
#    n = 5
#    -------------
#    0 0 0 0 0
#    0 0 1 0 0
#    0 1 0 1 0
#    0 1 1 1 0
#    1 0 0 0 1
#    1 0 1 0 1
#    1 1 0 1 1
#    1 1 1 1 1
#    ------------

if __name__ == "__main__": main()

#### Output

Given Length :  6
0   0   0   0   0   0
0   0   1   1   0   0
0   1   0   0   1   0
0   1   1   1   1   0
1   0   0   0   0   1
1   0   1   1   0   1
1   1   0   0   1   1
1   1   1   1   1   1

Given Length :  5
0   0   0   0   0
0   0   1   0   0
0   1   0   1   0
0   1   1   1   0
1   0   0   0   1
1   0   1   0   1
1   1   0   1   1
1   1   1   1   1
#  Ruby program for
#  Generate palindromic binary strings of given length N
class Combination
#  Display calculated result
def printResult(result, n)
i = 0
while (i < n)
print("  ", result[i])
i += 1
end

print("\n ")
end

def sequence(result, index, n)
if (index == n / 2)
if ((n % 2) != 0)
#  Handles the request of
#  odd length palindrome sequence
result[index] = '0'
self.printResult(result, n)
result[index] = '1'
self.printResult(result, n)
else

self.printResult(result, n)
end

elsif (index > n / 2)
return
else

#  Set the value of boundary element is to zero
result[index] = '0'
result[(n - 1) - index] = '0'
self.sequence(result, index + 1, n)
#  Set the value of boundary element is to one
result[index] = '1'
result[(n - 1) - index] = '1'
self.sequence(result, index + 1, n)
end

end

def palindrome(n)
if (n <= 0)
return
end

print("\n Given Length : ", n ,"\n ")
result = Array.new(n) { ' ' }
self.sequence(result, 0, n)
end

end

def main()
n = 6
#    Test A
#    n = 6
#    ----------------
#    0 0 0 0 0 0
#    0 0 1 1 0 0
#    0 1 0 0 1 0
#    0 1 1 1 1 0
#    1 0 0 0 0 1
#    1 0 1 1 0 1
#    1 1 0 0 1 1
#    1 1 1 1 1 1
#    -----------
n = 5
#    Test B
#    n = 5
#    -------------
#    0 0 0 0 0
#    0 0 1 0 0
#    0 1 0 1 0
#    0 1 1 1 0
#    1 0 0 0 1
#    1 0 1 0 1
#    1 1 0 1 1
#    1 1 1 1 1
#    ------------
end

main()

#### Output

Given Length : 6
0  0  0  0  0  0
0  0  1  1  0  0
0  1  0  0  1  0
0  1  1  1  1  0
1  0  0  0  0  1
1  0  1  1  0  1
1  1  0  0  1  1
1  1  1  1  1  1

Given Length : 5
0  0  0  0  0
0  0  1  0  0
0  1  0  1  0
0  1  1  1  0
1  0  0  0  1
1  0  1  0  1
1  1  0  1  1
1  1  1  1  1

// Scala program for
// Generate palindromic binary strings of given length N
class Combination()
{
// Display calculated result
def printResult(result: Array[Char], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print("  " + result(i));
i += 1;
}
print("\n ");
}
def sequence(result: Array[Char],
index: Int, n: Int): Unit = {
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
result(index) = '0';
printResult(result, n);
result(index) = '1';
printResult(result, n);
}
else
{
printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
// Set the value of boundary element is to zero
result(index) = '0';
result((n - 1) - index) = '0';
sequence(result, index + 1, n);
// Set the value of boundary element is to one
result(index) = '1';
result((n - 1) - index) = '1';
sequence(result, index + 1, n);
}
}
def palindrome(n: Int): Unit = {
if (n <= 0)
{
return;
}
print("\n Given Length : " + n + "\n ");
var result: Array[Char] = Array.fill[Char](n)(' ');
sequence(result, 0, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Combination = new Combination();
var n: Int = 6;
/*
Test A
n = 6
----------------
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1
-----------
*/
n = 5;
/*
Test B
n = 5
-------------
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
------------
*/
}
}

#### Output

Given Length : 6
0  0  0  0  0  0
0  0  1  1  0  0
0  1  0  0  1  0
0  1  1  1  1  0
1  0  0  0  0  1
1  0  1  1  0  1
1  1  0  0  1  1
1  1  1  1  1  1

Given Length : 5
0  0  0  0  0
0  0  1  0  0
0  1  0  1  0
0  1  1  1  0
1  0  0  0  1
1  0  1  0  1
1  1  0  1  1
1  1  1  1  1
// Swift 4 program for
// Generate palindromic binary strings of given length N
class Combination
{
// Display calculated result
func printResult(_ result: [Character], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print("  ", result[i], terminator: "");
i += 1;
}
print("\n ", terminator: "");
}
func sequence(_ result: inout[Character],
_ index: Int,
_ n: Int)
{
if (index == n / 2)
{
if ((n % 2)  != 0)
{
// Handles the request of
// odd length palindrome sequence
result[index] = "0";
self.printResult(result, n);
result[index] = "1";
self.printResult(result, n);
}
else
{
self.printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
// Set the value of boundary element is to zero
result[index] = "0";
result[(n - 1) - index] = "0";
self.sequence(&result, index + 1, n);
// Set the value of boundary element is to one
result[index] = "1";
result[(n - 1) - index] = "1";
self.sequence(&result, index + 1, n);
}
}
func palindrome(_ n: Int)
{
if (n <= 0)
{
return;
}
print("\n Given Length : ", n ,"\n ", terminator: "");
var result: [Character] = Array(repeating: " ", count: n);
self.sequence(&result, 0, n);
}
}
func main()
{
var n: Int = 6;
/*
Test A
n = 6
----------------
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1
-----------
*/
n = 5;
/*
Test B
n = 5
-------------
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
------------
*/
}
main();

#### Output

Given Length :  6
0   0   0   0   0   0
0   0   1   1   0   0
0   1   0   0   1   0
0   1   1   1   1   0
1   0   0   0   0   1
1   0   1   1   0   1
1   1   0   0   1   1
1   1   1   1   1   1

Given Length :  5
0   0   0   0   0
0   0   1   0   0
0   1   0   1   0
0   1   1   1   0
1   0   0   0   1
1   0   1   0   1
1   1   0   1   1
1   1   1   1   1
// Kotlin program for
// Generate palindromic binary strings of given length N
class Combination
{
// Display calculated result
fun printResult(result: Array < Char > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print("  " + result[i]);
i += 1;
}
print("\n ");
}
fun sequence(result: Array < Char > ,
index: Int,
n: Int): Unit
{
if (index == n / 2)
{
if ((n % 2) != 0)
{
// Handles the request of
// odd length palindrome sequence
result[index] = '0';
this.printResult(result, n);
result[index] = '1';
this.printResult(result, n);
}
else
{
this.printResult(result, n);
}
}
else if (index > n / 2)
{
return;
}
else
{
// Set the value of boundary element is to zero
result[index] = '0';
result[(n - 1) - index] = '0';
this.sequence(result, index + 1, n);
// Set the value of boundary element is to one
result[index] = '1';
result[(n - 1) - index] = '1';
this.sequence(result, index + 1, n);
}
}
fun palindrome(n: Int): Unit
{
if (n <= 0)
{
return;
}
print("\n Given Length : " + n + "\n ");
val result: Array < Char > = Array(n)
{
' '
};
this.sequence(result, 0, n);
}
}
fun main(args: Array < String > ): Unit
{
var n: Int = 6;
/*
Test A
n = 6
----------------
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 0 1 0
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 1
1 1 0 0 1 1
1 1 1 1 1 1
-----------
*/
n = 5;
/*
Test B
n = 5
-------------
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 1
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
------------
*/
}

#### Output

Given Length : 6
0  0  0  0  0  0
0  0  1  1  0  0
0  1  0  0  1  0
0  1  1  1  1  0
1  0  0  0  0  1
1  0  1  1  0  1
1  1  0  0  1  1
1  1  1  1  1  1

Given Length : 5
0  0  0  0  0
0  0  1  0  0
0  1  0  1  0
0  1  1  1  0
1  0  0  0  1
1  0  1  0  1
1  1  0  1  1
1  1  1  1  1

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.