Pattern of 1’s inside 0’s

Given the value of n, i.e, the number of rows/columns in a square, print the pattern.
Examples : 
 

Input : n = 8
Output :
 0 0 0 0 0 0 0 0
 0 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 0
 0 0 0 0 0 0 0 0

Input : n = 10
Output :
 0 0 0 0 0 0 0 0 0 0
 0 1 1 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 1 1 0
 0 0 0 0 0 0 0 0 0 0

 

 

C++




// C++ implementation of ones
// inside zeroes pattern
#include <bits/stdc++.h>
using namespace std;
  
// function to print the pattern
void print_pattern(int n)
{
    int k = 1, m = n;
    int zero = 0, one = 1;
  
    // for loop to keep track of
    // number of rows
    for (int i = 1; i <= n; i++) {
  
        // for loop to keep track of
        // number of columns
        for (int j = 1; j <= n; j++) {
  
            // if first row or last row
            if (i == k || i == m)
                cout << " " << zero;
  
            // if first column or last column
            else if (j == k || j == m)
                cout << " " << zero;
  
            // to print 1 in remaining portion
            else
                cout << " " << one;
        }
        cout << endl;
    }
}
  
// driver code
int main()
{
    // get value of n from user
    int n = 8;
  
    // function calling
    print_pattern(n);
  
    return 0;
}


Java




// Java implementation of ones
// inside zeroes pattern
class GFG
{
    // function to print the pattern
    static void print_pattern(int n)
    {
        int k = 1, m = n;
        int zero = 0, one = 1;
          
        // for loop to keep track of
        // number of rows
        for (int i = 1; i <= n; i++) {
              
            // for loop to keep track of
            // number of columns
            for (int j = 1; j <= n; j++) { 
                      
                // if first row or last row
                if (i == k || i == m)
                System.out.print(" "+zero); 
          
                // if first column or last column
                else if (j == k || j == m)
                System.out.print(" "+zero);
          
                // to print 1 in remaining portion
                else
                System.out.print(" "+one);
            }
          
        System.out.print("\n");
        }
    }
      
// Driver code
public static void main(String args[])
{
    // get value of n from user
    int n = 8;
      
    // function calling
    print_pattern(n);
}
}
  
//This code is written by Azkia Anam.


Python3




# Python 3 implementation of ones
# inside zeroes pattern
# function to print the pattern
  
def print_pattern(n):
    k = 1
    m = n
    zero = 0
    one = 1
  
    # for loop to keep track of
    # number of rows
    for i in range(1, n+1):
  
        # for loop to keep track of
        # number of columns
        for j in range(1,n+1):
  
            # if first row or last row
            if (i == k or i == m):
                print(" ",end="0")
  
            # if first column or last column
            elif (j == k or j == m):
                print(" ",end="0")
  
            # to print 1 in remaining portion
            else:
                print(" ",end="1")
  
        print("\r")
  
# driver code
  
# get value of n from user
n = 8
  
# function calling
print_pattern(n)
  
# This code is written
# by Azkia Anam.


C#




// C# implementation of ones
// inside zeroes pattern
using System;
  
class GFG
{
    // function to print the pattern
    static void print_pattern(int n)
    {
        int k = 1, m = n;
        int zero = 0, one = 1;
          
        // for loop to keep track of
        // number of rows
        for (int i = 1; i <= n; i++) {
              
            // for loop to keep track of
            // number of columns
            for (int j = 1; j <= n; j++) { 
                      
            // if first row or last row
            if (i == k || i == m)
            Console.Write(" "+zero); 
          
            // if first column or last column
            else if (j == k || j == m)
            Console.Write(" "+zero);
          
            // to print 1 in remaining portion
            else
            Console.Write(" "+one);
            }
          
        Console.WriteLine();
        }
    }
      
// driver code
public static void Main()
{
    // get value of n from user
    int n = 8;
      
    // function calling
    print_pattern(n);
}
}
  
//This code is written by vt_m.


PHP




<?php
// PHP implementation of ones
// inside zeroes pattern
  
// Function to print the pattern
function print_pattern($n)
{
    $k = 1;
    $m = $n;
    $zero = 0;
    $one = 1;
  
    // for loop to keep track of
    // number of rows
    for ($i = 1; $i <= $n; $i++) 
    {
  
        // for loop to keep track of
        // number of columns
        for ($j = 1; $j <= $n; $j++) 
        {
  
            // if first row or 
            // last row
            if ($i == $k || $i == $m)
                echo " ".$zero;
  
            // if first column or 
            // last column
            else if ($j == $k || 
                     $j == $m)
                echo " ".$zero;
  
            // to print 1 in 
            // remaining portion
            else
                echo " ".$one;
        }
        echo "\n";
    }
}
  
// Driver code
$n = 8;
print_pattern($n);
  
// This code is contributed by Mithun Kumar
?>


Javascript




<script>
  
// Javascript implementation of ones
// inside zeroes pattern
  
    // function to print the pattern
    function print_pattern(n)
    {
        let k = 1, m = n;
        let zero = 0, one = 1;
            
        // for loop to keep track of
        // number of rows
        for (let i = 1; i <= n; i++) {
                
            // for loop to keep track of
            // number of columns
            for (let j = 1; j <= n; j++) { 
                        
                // if first row or last row
                if (i == k || i == m)
                document.write(" "+zero); 
            
                // if first column or last column
                else if (j == k || j == m)
                document.write(" "+zero);
            
                // to print 1 in remaining portion
                else
                document.write(" "+one);
            }
            
        document.write("<br/>");
        }
    }  
  
  
// Driver Code
  
    // get value of n from user
    let n = 8;
        
    // function calling
    print_pattern(n);
  
</script>


Output :  

 0 0 0 0 0 0 0 0
 0 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 0
 0 1 1 1 1 1 1 0
 0 0 0 0 0 0 0 0

Time Complexity: O(n2)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button