Open In App

Program to print modified Binary triangle pattern

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: 
 

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

Below is the implementation of the above approach:
 




// C++ implementation to print the
// modified binary triangle pattern
 
#include <bits/stdc++.h>
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 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




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




<script>
 
// Javascript implementation to print the
// modified binary triangle pattern
 
// Function to print the modified
// binary pattern
function modifiedBinaryPattern(n)
{
   
    // Loop to traverse the rows
    for(let i = 1; i <= n; i++)
    {
          
       // Loop to traverse the
       // numbers in each row
       for(let j = 1; j <= i; j++)
       {
              
           // Check if j is 1 or i
           // In either case print 1
           if (j == 1 || j == i)
               document.write(1);
              
           // Else print 0
           else
               document.write(0);
        }
           
        // Change the cursor to next
        // line after each row
        document.write("<br/>");
    }
}
 
// Driver code
    let n = 7;
   
    // Function call
    modifiedBinaryPattern(n);
     
    // This code is contributed by susmitakundugoaldanga.
</script>

Output: 
1
11
101
1001
10001
100001
1000001

 

Time Complexity: O(n2)

Auxiliary Space: O(1)


Article Tags :