Print X inside rectangular box

X symbol inside rectangular box

Here given code implementation process.

//C Program 
//Print X inside rectangular box
#include <stdio.h>

//Print X star pattern of given height
void print_x(int height)
{
  if (height <= 2)
  {
    return;
  }
  printf("\nHeight : %d  \n\n", height);
  //Loop controlling variables
  int i = 0, j = 0;
  //Loop, which is control the printing row operations
  for (i = 0; i < height; ++i)
  {
    //Loop, which is control the printing column operations
    for (j = 0; j < height; ++j)
    {
      //Condition which is printing a star element
      if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
      {
        printf("* ");
      }
      else
      {
        printf("  ");
      }
    }
    printf("\n");
  }
}
int main()
{
  //Simple test
  print_x(9);
  print_x(7);
  print_x(10);
  return 0;
}

Output

Height : 9

* * * * * * * * *
* *           * *
*   *       *   *
*     *   *     *
*       *       *
*     *   *     *
*   *       *   *
* *           * *
* * * * * * * * *

Height : 7

* * * * * * *
* *       * *
*   *   *   *
*     *     *
*   *   *   *
* *       * *
* * * * * * *

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
/*
  Java Program
  Print X inside rectangular box
*/
class MyPattern
{
  //Print X star pattern of given height
  public void print_x(int height)
  {
    if (height <= 2)
    {
      return;
    }
    System.out.print("\nHeight : " + height + " \n\n");
    //Loop controlling variables
    int i = 0, j = 0;
    //Loop, which is control the printing row operations
    for (i = 0; i < height; ++i)
    {
      //Loop, which is control the printing column operations
      for (j = 0; j < height; ++j)
      {
        //Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        {
          System.out.print("* ");
        }
        else
        {
          System.out.print("  ");
        }
      }
      System.out.print("\n");
    }
  }
  public static void main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Simple test
    obj.print_x(9);
    obj.print_x(7);
    obj.print_x(10);
  }
}

Output

Height : 9

* * * * * * * * *
* *           * *
*   *       *   *
*     *   *     *
*       *       *
*     *   *     *
*   *       *   *
* *           * *
* * * * * * * * *

Height : 7

* * * * * * *
* *       * *
*   *   *   *
*     *     *
*   *   *   *
* *       * *
* * * * * * *

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
/*
  C++ Program
  Print X inside rectangular box
*/
#include<iostream>

using namespace std;
class MyPattern
{
  public:
    //Print X star pattern of given height
    void print_x(int height)
    {
      if (height <= 2)
      {
        return;
      }
      cout << "\nHeight : " << height << " \n\n";
      //Loop controlling variables
      int i = 0, j = 0;
      //Loop, which is control the printing row operations
      for (i = 0; i < height; ++i)
      {
        //Loop, which is control the printing column operations
        for (j = 0; j < height; ++j)
        {
          //Condition which is printing a star element
          if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
          {
            cout << "* ";
          }
          else
          {
            cout << "  ";
          }
        }
        cout << "\n";
      }
    }
};
int main()
{
  MyPattern obj;
  //Simple test
  obj.print_x(9);
  obj.print_x(7);
  obj.print_x(10);
  return 0;
}

Output

Height : 9

* * * * * * * * *
* *           * *
*   *       *   *
*     *   *     *
*       *       *
*     *   *     *
*   *       *   *
* *           * *
* * * * * * * * *

Height : 7

* * * * * * *
* *       * *
*   *   *   *
*     *     *
*   *   *   *
* *       * *
* * * * * * *

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
/*
  C# Program
  Print X inside rectangular box
*/
using System;
class MyPattern
{
  //Print X star pattern of given height
  public void print_x(int height)
  {
    if (height <= 2)
    {
      return;
    }
    Console.Write("\nHeight : " + height + " \n\n");
    //Loop controlling variables
    int i = 0, j = 0;
    //Loop, which is control the printing row operations
    for (i = 0; i < height; i++)
    {
      //Loop, which is control the printing column operations
      for (j = 0; j < height; j++)
      {
        //Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        {
          Console.Write("* ");
        }
        else
        {
          Console.Write("  ");
        }
      }
      Console.Write("\n");
    }
  }
  public static void Main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Simple test
    obj.print_x(9);
    obj.print_x(7);
    obj.print_x(10);
  }
}

Output

Height : 9

* * * * * * * * *
* *           * *
*   *       *   *
*     *   *     *
*       *       *
*     *   *     *
*   *       *   *
* *           * *
* * * * * * * * *

Height : 7

* * * * * * *
* *       * *
*   *   *   *
*     *     *
*   *   *   *
* *       * *
* * * * * * *

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
<?php
/*
  Php Program
  Print X inside rectangular box
*/
class MyPattern
{
  //Print X star pattern of given height
  function print_x($height)
  {
    if ($height <= 2)
    {
      return;
    }
    echo "\nHeight : ". $height ." \n\n";
    //Loop controlling variables
    $i = 0;
    $j = 0;
    //Loop, which is control the printing row operations
    for ($i = 0; $i < $height; ++$i)
    {
      //Loop, which is control the printing column operations
      for ($j = 0; $j < $height; ++$j)
      {
        //Condition which is printing a star element
        if ($i == 0 || $j == $i || $i == $height - 1 || $height - 1 - $i == $j || $j == 0 || $j == $height - 1)
        {
          echo "* ";
        }
        else
        {
          echo "  ";
        }
      }
      echo "\n";
    }
  }
}

function main()
{
  $obj = new MyPattern();
  //Simple test
  $obj->print_x(9);
  $obj->print_x(7);
  $obj->print_x(10);
}
main();

Output

Height : 9

* * * * * * * * *
* *           * *
*   *       *   *
*     *   *     *
*       *       *
*     *   *     *
*   *       *   *
* *           * *
* * * * * * * * *

Height : 7

* * * * * * *
* *       * *
*   *   *   *
*     *     *
*   *   *   *
* *       * *
* * * * * * *

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
/*
  Node Js Program
  Print X inside rectangular box
*/
class MyPattern
{
  //Print X star pattern of given height
  print_x(height)
  {
    if (height <= 2)
    {
      return;
    }
    process.stdout.write("\nHeight : " + height + " \n\n");
    //Loop controlling variables
    var i = 0;
    var j = 0;
    //Loop, which is control the printing row operations
    for (i = 0; i < height; ++i)
    {
      //Loop, which is control the printing column operations
      for (j = 0; j < height; ++j)
      {
        //Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        {
          process.stdout.write("* ");
        }
        else
        {
          process.stdout.write("  ");
        }
      }
      process.stdout.write("\n");
    }
  }
}

function main()
{
  var obj = new MyPattern();
  //Simple test
  obj.print_x(9);
  obj.print_x(7);
  obj.print_x(10);
}
main();

Output

Height : 9

* * * * * * * * *
* *           * *
*   *       *   *
*     *   *     *
*       *       *
*     *   *     *
*   *       *   *
* *           * *
* * * * * * * * *

Height : 7

* * * * * * *
* *       * *
*   *   *   *
*     *     *
*   *   *   *
* *       * *
* * * * * * *

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
#   Python 3 Program
#   Print X inside rectangular box

class MyPattern :
  # Print X star pattern of given height
  def print_x(self, height) :
    if (height <= 2) :
      return
    
    print("\nHeight : ", height ," \n\n", end = "")
    # Loop controlling variables
    i = 0
    j = 0
    # Loop, which is control the printing row operations
    while (i < height) :
      # Loop, which is control the printing column operations
      j = 0
      while (j < height) :
        # Condition which is printing a star element
        if (i == 0 or j == i or i == height - 1 or height - 1 - i == j or j == 0 or j == height - 1) :
          print("* ", end = "")
        else :
          print("  ", end = "")
        
        j += 1
      
      print("\n", end = "")
      i += 1
    
  

def main() :
  obj = MyPattern()
  # Simple test
  obj.print_x(9)
  obj.print_x(7)
  obj.print_x(10)

if __name__ == "__main__": main()

Output

Height :  9

* * * * * * * * *
* *           * *
*   *       *   *
*     *   *     *
*       *       *
*     *   *     *
*   *       *   *
* *           * *
* * * * * * * * *

Height :  7

* * * * * * *
* *       * *
*   *   *   *
*     *     *
*   *   *   *
* *       * *
* * * * * * *

Height :  10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
#   Ruby Program
#   Print X inside rectangular box

class MyPattern

  # Print X star pattern of given height
  def print_x(height)
  
    if (height <= 2)
    
      return
    end
    print("\nHeight : ", height ," \n\n")
    # Loop controlling variables
    i = 0
    j = 0
    # Loop, which is control the printing row operations
    while (i < height)
    
      # Loop, which is control the printing column operations
      j = 0
      while (j < height)
      
        # Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        
          print("* ")
        else
        
          print("  ")
        end
        j += 1
      end
      print("\n")
      i += 1
    end
  end
end
def main()

  obj = MyPattern.new()
  # Simple test
  obj.print_x(9)
  obj.print_x(7)
  obj.print_x(10)
end
main()

Output

Height : 9 

* * * * * * * * * 
* *           * * 
*   *       *   * 
*     *   *     * 
*       *       * 
*     *   *     * 
*   *       *   * 
* *           * * 
* * * * * * * * * 

Height : 7 

* * * * * * * 
* *       * * 
*   *   *   * 
*     *     * 
*   *   *   * 
* *       * * 
* * * * * * * 

Height : 10 

* * * * * * * * * * 
* *             * * 
*   *         *   * 
*     *     *     * 
*       * *       * 
*       * *       * 
*     *     *     * 
*   *         *   * 
* *             * * 
* * * * * * * * * * 
/*
  Scala Program
  Print X inside rectangular box
*/
class MyPattern
{
  //Print X star pattern of given height
  def print_x(height: Int): Unit = {
    if (height <= 2)
    {
      return;
    }
    print("\nHeight : " + height + " \n\n");
    //Loop controlling variables
    var i: Int = 0;
    var j: Int = 0;
    //Loop, which is control the printing row operations
    while (i < height)
    {
      //Loop, which is control the printing column operations
      j = 0;
      while (j < height)
      {
        //Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        {
          print("* ");
        }
        else
        {
          print("  ");
        }
        j += 1;
      }
      print("\n");
      i += 1;
    }
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();
    //Simple test
    obj.print_x(9);
    obj.print_x(7);
    obj.print_x(10);
  }
}

Output

Height : 9

* * * * * * * * *
* *           * *
*   *       *   *
*     *   *     *
*       *       *
*     *   *     *
*   *       *   *
* *           * *
* * * * * * * * *

Height : 7

* * * * * * *
* *       * *
*   *   *   *
*     *     *
*   *   *   *
* *       * *
* * * * * * *

Height : 10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *
/*
  Swift Program
  Print X inside rectangular box
*/
class MyPattern
{
  //Print X star pattern of given height
  func print_x(_ height: Int)
  {
    if (height <= 2)
    {
      return;
    }
    print("\nHeight : ", height ," \n\n", terminator: "");
    //Loop controlling variables
    var i: Int = 0;
    var j: Int = 0;
    //Loop, which is control the printing row operations
    while (i < height)
    {
      //Loop, which is control the printing column operations
      j = 0;
      while (j < height)
      {
        //Condition which is printing a star element
        if (i == 0 || j == i || i == height - 1 || height - 1 - i == j || j == 0 || j == height - 1)
        {
          print("* ", terminator: "");
        }
        else
        {
          print("  ", terminator: "");
        }
        j += 1;
      }
      print("\n", terminator: "");
      i += 1;
    }
  }
}
func main()
{
  let obj: MyPattern = MyPattern();
  //Simple test
  obj.print_x(9);
  obj.print_x(7);
  obj.print_x(10);
}
main();

Output

Height :  9

* * * * * * * * *
* *           * *
*   *       *   *
*     *   *     *
*       *       *
*     *   *     *
*   *       *   *
* *           * *
* * * * * * * * *

Height :  7

* * * * * * *
* *       * *
*   *   *   *
*     *     *
*   *   *   *
* *       * *
* * * * * * *

Height :  10

* * * * * * * * * *
* *             * *
*   *         *   *
*     *     *     *
*       * *       *
*       * *       *
*     *     *     *
*   *         *   *
* *             * *
* * * * * * * * * *


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved