Program to print modified Binary triangle pattern

12 Nov, 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: 1 11 101 1001 10001 100001Input: N = 3 Output: 1 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++
Java
Python 3
C#
Javascript
// C++ implementation to print the// modified binary triangle pattern #include <bits/stdc++.h>using namespace std; // Function to print the modified// binary patternvoid 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 Codeint main(){    int n = 7;     // Function Call    modifiedBinaryPattern(n);}
// Java implementation to print the// modified binary triangle patternimport java.io.*; class GFG{ // Function to print the modified// binary patternstatic 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 Codepublic 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 patterndef 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 Codeif __name__ == '__main__':         n = 7         # Function Call    modifiedBinaryPattern(n) # This code is contributed by Samarth
// C# implementation to print the// modified binary triangle patternusing System;class GFG{ // Function to print the modified// binary patternstatic 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 Codepublic 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 patternfunction 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>
1
11
101
1001
10001
100001
1000001
 
Time Complexity: O(n2)
Auxiliary Space: O(1)
ipg2016107
SHUBHAMSINGH10
nidhi_biet
susmitakundugoaldanga
subham348
binary-string
pattern-printing
Mathematical
School Programming
Write From Home
Mathematical
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.