# Print modified binary triangle pattern

In this article, we will discuss the problem of printing a modified binary triangle pattern. We will provide an explanation of the problem statement, a suitable example, the algorithm, pseudocode, and the resultant output explanation. The time complexity of the code will also be analyzed.

### Introduction

The problem involves printing a triangular pattern consisting of binary digits. Each row of the triangle has a height equal to the row number. The pattern starts with a single '1' at the top, and the remaining elements in each row are either '1' or '0'. The first and last elements in each row are '1', while the intermediate elements are '0'.

### Problem Statement

The task is to print a modified binary triangle pattern of a given height. The height determines the number of rows in the pattern, and each row contains a different number of elements. The elements are either '1' or '0', following the rules mentioned above.

Let's take an example to illustrate the problem. For a height of 7, the pattern looks like this:

```1
11
101
1001
10001
100001
1000001
```

In this example, the triangle has 7 rows, and each row has a different number of elements. The first and last elements in each row are '1', while the intermediate elements are '0'.

### Algorithm and Pseudocode

To solve this problem, we can use a nested loop structure. The outer loop iterates through each row, and the inner loop prints the elements of each row. The logic inside the inner loop determines whether to print '1' or '0' based on the position of the element.

Here is the algorithm to print the modified binary triangle pattern:

1. Start
2. Take the input height of the triangle
3. For i = 1 to height, do
1. For j = 1 to i, do
1. If j = 1 or j = i, then
1. Print '1'
2. Else, then
1. Print '0'
2. Print a new line
4. End

The corresponding pseudocode for the algorithm is as follows:

``````
function printTriangle(height)
print "Given height:", height
for i = 1 to height do
for j = 1 to i do
if j = 1 or j = i then
print "1"
else
print "0"
end if
end for
print new line
end for
end function```
```

## Code Solution

``````//  C program for
//  Print modified binary triangle pattern
#include <stdio.h>

void printTriangle(int height)
{
printf("\nGiven height : %d \n", height);

for (int i = 1; i <= height; ++i)
{
for (int j = 1; j <= i; ++j)
{
if (j == 1 || j == i)
{
// Corner element is 1
printf("1");
}
else
{
// Intermediate element is 0
printf("0");
}
}
printf("\n");
}
}
int main(int argc, char const *argv[])
{
// Test
printTriangle(7);
printTriangle(12);
return 0;
}``````

#### Output

``````Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````
``````/*
Java Program
Print modified binary triangle pattern
*/
public class Pattern
{
public void printTriangle(int height)
{
System.out.println("\nGiven height : " + height);
// Outer loop control the row operation
for (int i = 1; i <= height; ++i)
{
// Inner loop control the column operation
for (int j = 1; j <= i; ++j)
{
if (j == 1 || j == i)
{
// Corner element is 1
System.out.print("1");
}
else
{
// Intermediate element is 0
System.out.print("0");
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
// Test
}
}``````

#### Output

``````Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Print modified binary triangle pattern
*/
class Pattern
{
public: void printTriangle(int height)
{
cout << "\nGiven height : " << height << endl;
// Outer loop control the row operation
for (int i = 1; i <= height; ++i)
{
// Inner loop control the column operation
for (int j = 1; j <= i; ++j)
{
if (j == 1 || j == i)
{
// Corner element is 1
cout << "1";
}
else
{
// Intermediate element is 0
cout << "0";
}
}
cout << "\n";
}
}
};
int main()
{
// Test
return 0;
}``````

#### Output

``````Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````
``````// Include namespace system
using System;
/*
Csharp Program
Print modified binary triangle pattern
*/
public class Pattern
{
public void printTriangle(int height)
{
Console.WriteLine("\nGiven height : " + height);
// Outer loop control the row operation
for (int i = 1; i <= height; ++i)
{
// Inner loop control the column operation
for (int j = 1; j <= i; ++j)
{
if (j == 1 || j == i)
{
// Corner element is 1
Console.Write("1");
}
else
{
// Intermediate element is 0
Console.Write("0");
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
// Test
}
}``````

#### Output

``````Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````
``````package main
import "fmt"
/*
Go Program
Print modified binary triangle pattern
*/
type Pattern struct {}
func getPattern() * Pattern {
var me *Pattern = &Pattern {}
return me
}
func(this Pattern) printTriangle(height int) {
fmt.Println("\nGiven height : ", height)
// Outer loop control the row operation
for i := 1 ; i <= height ; i++ {
// Inner loop control the column operation
for j := 1 ; j <= i ; j++ {
if j == 1 || j == i {
// Corner element is 1
fmt.Print("1")
} else {
// Intermediate element is 0
fmt.Print("0")
}
}
fmt.Print("\n")
}
}
func main() {
var task * Pattern = getPattern()
// Test
}``````

#### Output

``````Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````
``````<?php
/*
Php Program
Print modified binary triangle pattern
*/
class Pattern
{
public	function printTriangle(\$height)
{
echo("\nGiven height : ".\$height."\n");
// Outer loop control the row operation
for (\$i = 1; \$i <= \$height; ++\$i)
{
// Inner loop control the column operation
for (\$j = 1; \$j <= \$i; ++\$j)
{
if (\$j == 1 || \$j == \$i)
{
// Corner element is 1
echo("1");
}
else
{
// Intermediate element is 0
echo("0");
}
}
echo("\n");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

``````Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````
``````/*
Node JS Program
Print modified binary triangle pattern
*/
class Pattern
{
printTriangle(height)
{
console.log("\nGiven height : " + height);
// Outer loop control the row operation
for (var i = 1; i <= height; ++i)
{
// Inner loop control the column operation
for (var j = 1; j <= i; ++j)
{
if (j == 1 || j == i)
{
// Corner element is 1
process.stdout.write("1");
}
else
{
// Intermediate element is 0
process.stdout.write("0");
}
}
process.stdout.write("\n");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

``````Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````
``````#    Python 3 Program
#    Print modified binary triangle pattern
class Pattern :
def printTriangle(self, height) :
print("\nGiven height : ", height)
i = 1
#  Outer loop control the row operation
while (i <= height) :
j = 1
#  Inner loop control the column operation
while (j <= i) :
if (j == 1 or j == i) :
#  Corner element is 1
print("1", end = "")
else :
#  Intermediate element is 0
print("0", end = "")

j += 1

print(end = "\n")
i += 1

def main() :
#  Test

if __name__ == "__main__": main()``````

#### Output

``````Given height :  7
1
11
101
1001
10001
100001
1000001

Given height :  12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````
``````#    Ruby Program
#    Print modified binary triangle pattern
class Pattern
def printTriangle(height)
print("\nGiven height : ", height, "\n")
i = 1
#  Outer loop control the row operation
while (i <= height)
j = 1
#  Inner loop control the column operation
while (j <= i)
if (j == 1 || j == i)
#  Corner element is 1
print("1")
else

#  Intermediate element is 0
print("0")
end

j += 1
end

print("\n")
i += 1
end

end

end

def main()
#  Test
end

main()``````

#### Output

``````Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
``````
``````/*
Scala Program
Print modified binary triangle pattern
*/
class Pattern()
{
def printTriangle(height: Int): Unit = {
println("\nGiven height : " + height);
var i: Int = 1;
// Outer loop control the row operation
while (i <= height)
{
var j: Int = 1;
// Inner loop control the column operation
while (j <= i)
{
if (j == 1 || j == i)
{
// Corner element is 1
print("1");
}
else
{
// Intermediate element is 0
print("0");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pattern = new Pattern();
// Test
}
}``````

#### Output

``````Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````
``````/*
Swift 4 Program
Print modified binary triangle pattern
*/
class Pattern
{
func printTriangle(_ height: Int)
{
print("\nGiven height : ", height);
var i: Int = 1;
// Outer loop control the row operation
while (i <= height)
{
var j: Int = 1;
// Inner loop control the column operation
while (j <= i)
{
if (j == 1 || j == i)
{
// Corner element is 1
print("1", terminator: "");
}
else
{
// Intermediate element is 0
print("0", terminator: "");
}
j += 1;
}
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
// Test
}
main();``````

#### Output

``````Given height :  7
1
11
101
1001
10001
100001
1000001

Given height :  12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````
``````/*
Kotlin Program
Print modified binary triangle pattern
*/
class Pattern
{
fun printTriangle(height: Int): Unit
{
println("\nGiven height : " + height);
var i: Int = 1;
// Outer loop control the row operation
while (i <= height)
{
var j: Int = 1;
// Inner loop control the column operation
while (j <= i)
{
if (j == 1 || j == i)
{
// Corner element is 1
print("1");
}
else
{
// Intermediate element is 0
print("0");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

#### Output

``````Given height : 7
1
11
101
1001
10001
100001
1000001

Given height : 12
1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001``````

### Resultant Output Explanation

Now, let's understand the resultant output for the given examples.

For the height of 7, the pattern is:

```1
11
101
1001
10001
100001
1000001
```

In this output, each row represents a row in the triangle. The first and last elements in each row are '1', while the intermediate elements are '0', following the given rules. The number of elements in each row corresponds to the row number.

Similarly, for the height of 12, the pattern is:

```1
11
101
1001
10001
100001
1000001
10000001
100000001
1000000001
10000000001
100000000001
```

This output follows the same pattern as the previous example, with the height determining the number of rows in the triangle.

### Time Complexity

The time complexity of the code can be analyzed as follows:

The outer loop runs 'height' number of times, and the inner loop runs from 1 to 'i', where 'i' is the current row number. Therefore, the total number of iterations of the inner loop is 1 + 2 + 3 + ... + height, which can be approximated to height * (height + 1) / 2.

Hence, the time complexity of the code is O(height^2).

### Finally

In this article, we discussed the problem of printing a modified binary triangle pattern. We provided an explanation of the problem statement, a suitable example, the algorithm, pseudocode, and the resultant output explanation. The time complexity of the code was analyzed to determine its efficiency. By understanding the approach and following the provided pseudocode, you can easily implement this pattern in various programming languages.

## 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.