Program to print modified Binary triangle pattern
Last Updated :
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
100001
Input: 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++
// 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
// 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
<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)
Similar Reads
Program to print a rectangle pattern Given height h and width w, print a rectangular pattern as shown in the example below. Examples: Input : h = 4, w = 5 Output : @@@@@ @ @ @ @ @@@@@ Input : h = 7, w = 9 Output : @@@@@@@@ @ @ @ @ @ @ @ @ @ @ @@@@@@@@ The idea is to run two loops. One for the number of rows to be printed and the other
4 min read
Program to print Sum Triangle for a given array Given a array, write a program to construct a triangle where last row contains elements of given array, every element of second last row contains sum of below two elements and so on. Example: Input: arr[] = {4, 7, 3, 6, 7};Output:8140 4121 19 2211 10 9 134 7 3 6 7 Input: {10, 40, 50}Output:14050 901
9 min read
Programs to print Triangle and Diamond patterns using recursion This article is aimed at giving a recursive implementation for pattern printing. Pattern 1: Example:Input: 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Implementation: C++ // Program to print the given pattern #include <iostream> using namespace std; void print_asterisk
15+ min read
Program to print the arrow pattern Given the value of n, print the arrow pattern.Examples : Input : n = 5 Output : * ** *** **** ***** **** *** ** * Input : n = 7 Output : * ** *** **** ***** ****** ******* ****** ***** **** *** ** * Below is the program to print the arrow pattern: C++ // C++ program to print the // arrow pattern #in
10 min read
Program to Print a Pattern of Numbers The idea of pattern based programs is to understand the concept of nesting of for loops and how and where to place the alphabets / numbers / stars to make the desired pattern.Write to program to print the pattern of numbers in the following manner using for loop 1 232 34543 4567654567898765In almost
8 min read