Related Articles

# Program to print modified Binary triangle pattern

• Difficulty Level : Easy
• Last Updated : 19 Apr, 2021

Given an integer N, the task is to print the modified binary tree pattern.

In Modified Binary Triangle Pattern, the first and last element of a Nth row are 1 and the middle (N – 2) elements are 0.

Examples:

Input: N = 6
Output:

11
101
1001
10001
100001
Input: N = 3
Output:

11
101

Approach: From the above examples, it can be observed that, for each row N:

• 1st and Nth element is a 1
• and elements 2 to (N-1) are 0

Therefore an algorithm can be developed to print such pattern as:

• Run a loop from 1 to N using a loop variable i, which denotes the row number of the triangle pattern.
• For each row i, run a loop from 1 to i, using a loop variable j, which denotes the number in each row.
• In each iteration of j, check if j is 1 or i. If either of it true, print 1.
• If none of the case is true for j, then print 0
• Increment the value of j by 1 after each iteration
• When the j loop has completed successfully, we have printed a row of the pattern. Therefore change the output to the next line by printing a next line.
• Increment the value of i and repeat the whole process till N rows has been printed successfully.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to print the``// modified binary triangle pattern` `#include ``using` `namespace` `std;` `// Function to print the modified``// binary pattern``void` `modifiedBinaryPattern(``int` `n)``{` `    ``// Loop to traverse the rows``    ``for` `(``int` `i = 1; i <= n; i++) {` `        ``// Loop to traverse the``        ``// numbers in each row``        ``for` `(``int` `j = 1; j <= i; j++) {` `            ``// Check if j is 1 or i``            ``// In either case print 1``            ``if` `(j == 1 || j == i)``                ``cout << 1;` `            ``// Else print 0``            ``else``                ``cout << 0;``        ``}` `        ``// Change the cursor to next``        ``// line after each row``        ``cout << endl;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `n = 7;` `    ``// Function Call``    ``modifiedBinaryPattern(n);``}`

## Java

 `// Java implementation to print the``// modified binary triangle pattern``import` `java.io.*;` `class` `GFG{` `// Function to print the modified``// binary pattern``static` `void` `modifiedBinaryPattern(``int` `n)``{` `    ``// Loop to traverse the rows``    ``for``(``int` `i = ``1``; i <= n; i++)``    ``{``       ` `       ``// Loop to traverse the``       ``// numbers in each row``       ``for``(``int` `j = ``1``; j <= i; j++)``       ``{``           ` `           ``// Check if j is 1 or i``           ``// In either case print 1``           ``if` `(j == ``1` `|| j == i)``               ``System.out.print(``1``);``           ` `           ``// Else print 0``           ``else``               ``System.out.print(``0``);``        ``}``        ` `        ``// Change the cursor to next``        ``// line after each row``        ``System.out.println();``    ``}``}` `// Driver Code``public` `static` `void` `main (String[] args)``{``    ``int` `n = ``7``;` `    ``// Function call``    ``modifiedBinaryPattern(n);``}``}` `// This code is contributed by shubhamsingh10`

## Python 3

 `# python3 implementation to print the``# modified binary triangle pattern` `# Function to print the modified``# binary pattern``def` `modifiedBinaryPattern(n):``    ` `    ``# Loop to traverse the rows``    ``for` `i ``in` `range``(``1``, n ``+` `1``, ``1``):` `        ``# Loop to traverse the``        ``# numbers in each row``        ``for` `j ``in` `range``(``1``, i ``+` `1``, ``1``):``            ` `            ``# Check if j is 1 or i``            ``# In either case print 1``            ``if` `(j ``=``=` `1` `or` `j ``=``=` `i):``                ``print``(``1``, end ``=` `"")` `            ``# Else print 0``            ``else``:``                ``print``(``0``, end ``=` `"")` `        ``# Change the cursor to next``        ``# line after each row``        ``print``(``'\n'``, end ``=` `"")` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``n ``=` `7``    ` `    ``# Function Call``    ``modifiedBinaryPattern(n)` `# This code is contributed by Samarth`

## C#

 `// C# implementation to print the``// modified binary triangle pattern``using` `System;``class` `GFG{` `// Function to print the modified``// binary pattern``static` `void` `modifiedBinaryPattern(``int` `n)``{` `    ``// Loop to traverse the rows``    ``for``(``int` `i = 1; i <= n; i++)``    ``{``        ` `        ``// Loop to traverse the``        ``// numbers in each row``        ``for``(``int` `j = 1; j <= i; j++)``        ``{``            ` `            ``// Check if j is 1 or i``            ``// In either case print 1``            ``if` `(j == 1 || j == i)``                ``Console.Write(1);``                ` `            ``// Else print 0``            ``else``                ``Console.Write(0);``        ``}``        ` `        ``// Change the cursor to next``        ``// line after each row``        ``Console.WriteLine();``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `n = 7;` `    ``// Function call``    ``modifiedBinaryPattern(n);``}``}` `// This code is contributed by Nidhi_Biet`

## Javascript

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

Attention reader! All those who say programming isn’t for kids, just haven’t met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12.

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.

My Personal Notes arrow_drop_up