Print X patterns

Here given code implementation process.

//C Program
//Print X pattern
#include <stdio.h>

void space(int size)
{
  for (int i = 0; i < size; ++i)
  {
    printf(" ");
  }
}
void show_pattern(int size)
{
  if(size<=0 || size %2 == 0)
  {
    //When size is Odd or less than zero
    return;
  }
  printf("Size : %d  \n\n",size );

  //Variable which is manage display value
  int a = 0 , b = size - 1; 

  for (int i = 0; i < size; ++i)
  {
    for (int j = 0; j < size; ++j)
    {
      if(j == a )
      {
        printf("*");
      }
      else if(j==b)
      {
        printf("*");
      }
      else
      {
        printf(" ");
      }
    }
    a++;
    b--;
    printf("\n");
  }
}

int main()
{
  //Test Case
  show_pattern(5);
  show_pattern(7);
  show_pattern(3);
  
  return 0;
}

Output

Size : 5

*   *
 * *
  *
 * *
*   *
Size : 7

*     *
 *   *
  * *
   *
  * *
 *   *
*     *
Size : 3

* *
 *
* *
/*
  C++ Program
  Print X pattern
*/
#include<iostream>

using namespace std;
class MyPattern {
  public:
    void space(int size) {
      for (int i = 0; i < size; ++i) {
        cout << " ";
      }
    }
  void show_pattern(int size) {
    if (size <= 0 ||
      size % 2 == 0) {
      return;
    }
    cout << "Size : " << size << " \n\n";
    //Variable which is manage display value
    int a = 0, b = size - 1;
    for (int i = 0; i < size; ++i) {
      for (int j = 0; j < size; ++j) {
        if (j == a) {
          cout << "*";
        } else
        if (j == b) {
          cout << "*";
        } else {
          cout << " ";
        }
      }
      a++;
      b--;
      cout << "\n";
    }
    cout << "\n";
  }
};
int main() {
  MyPattern obj =  MyPattern();
  //Test Case
  obj.show_pattern(5);
  obj.show_pattern(7);
  obj.show_pattern(3);
  return 0;
}

Output

Size : 5

*   *
 * *
  *
 * *
*   *

Size : 7

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

Size : 3

* *
 *
* *
/*
  Java Program
  Print X pattern
*/

public class MyPattern {

  public void space(int size) 
  {
    for (int i = 0; i < size; ++i) 
    {
      System.out.print(" ");
    }
  }
  public void show_pattern(int size) 
  {
    if (size <= 0 || size%2 == 0) 
    {
      return;
    }
    System.out.print("Size : "+size+" \n\n");
    //Variable which is manage display value
    int a = 0, b = size - 1;
    for (int i = 0; i < size; ++i) 
    {
      for (int j = 0; j < size; ++j) 
      {
        if (j == a) 
        {
          System.out.print("*");
        } 
        else if (j == b) 
        {
          System.out.print("*");
        } 
        else 
        {
          System.out.print(" ");
        }
      }
      a++;
      b--;
      System.out.print("\n");
    }
    System.out.print("\n");
  }
  public static void main(String[] args) {

    MyPattern obj = new MyPattern();
    //Test Case
    obj.show_pattern(5);
    obj.show_pattern(7);
    obj.show_pattern(3);

  }
}

Output

Size : 5

*   *
 * *
  *
 * *
*   *

Size : 7

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

Size : 3

* *
 *
* *
/*
  C# Program
  Print X pattern
*/
using System;
public class MyPattern {
  public void space(int size) {
    for (int i = 0; i < size; ++i) {
      Console.Write(" ");
    }
  }
  public void show_pattern(int size) {
    if (size <= 0 ||
      size % 2 == 0) {
      return;
    }
    Console.Write("Size : " + size + " \n\n");
    //Variable which is manage display value
    int a = 0, b = size - 1;
    for (int i = 0; i < size; ++i) {
      for (int j = 0; j < size; ++j) {
        if (j == a) {
          Console.Write("*");
        } else
        if (j == b) {
          Console.Write("*");
        } else {
          Console.Write(" ");
        }
      }
      a++;
      b--;
      Console.Write("\n");
    }
    Console.Write("\n");
  }
  public static void Main(String[] args) {
    MyPattern obj = new MyPattern();
    obj.show_pattern(5);
    obj.show_pattern(7);
    obj.show_pattern(3);
  }
}

Output

Size : 5

*   *
 * *
  *
 * *
*   *

Size : 7

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

Size : 3

* *
 *
* *
<?php
/*
  Php Program
  Print X pattern
*/
class MyPattern {
  public  function space($size) {
    for ($i = 0; $i < $size; ++$i) {
      echo(" ");
    }
  }
  public  function show_pattern($size) {
    if ($size <= 0 ||
      $size % 2 == 0) {
      return;
    }
    echo("Size : ". $size ." \n\n");
    //Variable which is manage display value
    $a = 0;
    $b = $size - 1;
    for ($i = 0; $i < $size; ++$i) {
      for ($j = 0; $j < $size; ++$j) {
        if ($j == $a) {
          echo("*");
        } else
        if ($j == $b) {
          echo("*");
        } else {
          echo(" ");
        }
      }
      $a++;
      $b--;
      echo("\n");
    }
    echo("\n");
  }
}

function main() {
  $obj = new MyPattern();
  //Test Case
  $obj->show_pattern(5);
  $obj->show_pattern(7);
  $obj->show_pattern(3);

}
main();

Output

Size : 5

*   *
 * *
  *
 * *
*   *

Size : 7

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

Size : 3

* *
 *
* *
/*
  Node Js Program
  Print X pattern
*/
class MyPattern {
  space(size) {
    for (var i = 0; i < size; ++i) {
      process.stdout.write(" ");
    }
  }
  show_pattern(size) {
    if (size <= 0 ||
      size % 2 == 0) {
      return;
    }

    process.stdout.write("Size : " + size + " \n\n");
    //Variable which is manage display value
    var a = 0;
    var b = size - 1;
    for (var i = 0; i < size; ++i) {
      for (var j = 0; j < size; ++j) {
        if (j == a) {
          process.stdout.write("*");
        } else
        if (j == b) {
          process.stdout.write("*");
        } else {
          process.stdout.write(" ");
        }
      }
      a++;
      b--;
      process.stdout.write("\n");
    }

    process.stdout.write("\n");
  }
}

function main(args) {
  var obj = new MyPattern();
  //Test Case
  obj.show_pattern(5);
  obj.show_pattern(7);
  obj.show_pattern(3);
}

main();

Output

Size : 5

*   *
 * *
  *
 * *
*   *

Size : 7

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

Size : 3

* *
 *
* *
#   Python 3 Program
#   Print X pattern

class MyPattern :
  def space(self, size) :
    i = 0
    while (i < size) :
      print(" ", end = "")
      i += 1
    
  
  def show_pattern(self, size) :
    if (size <= 0 or size % 2 == 0) :
      return
    
    print("Size : ", size ," \n\n", end = "")
    # Variable which is manage display value
    a = 0
    b = size - 1
    i = 0
    while (i < size) :
      j = 0
      while (j < size) :
        if (j == a) :
          print("*", end = "")
        elif (j == b) :
          print("*", end = "")
        else :
          print(" ", end = "")
        
        j += 1
      
      a += 1
      b -= 1
      print("\n", end = "")
      i += 1
    
    print("\n", end = "")
  

def main() :
  obj = MyPattern()
  # Test Case
  obj.show_pattern(5)
  obj.show_pattern(7)
  obj.show_pattern(3)


if __name__ == "__main__":
  main()

Output

Size :  5

*   *
 * *
  *
 * *
*   *

Size :  7

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

Size :  3

* *
 *
* *
#   Ruby Program
#   Print X pattern

class MyPattern 
  def space(size) 
    i = 0
    while (i < size) 
      print(" ")
      i += 1
    end
  end
  def show_pattern(size) 
    if (size <= 0 ||
      size % 2 == 0) 
      return
    end
    print("Size  :", size ," \n\n")
    # Variable which is manage display value
    a = 0
    b = size - 1
    i = 0
    while (i < size) 
      j = 0
      while (j < size) 
        if (j == a) 
          print("*")
        elsif (j == b) 
          print("*")
        else 
          print(" ")
        end
        j += 1
      end
      a += 1
      b -= 1
      print("\n")
      i += 1
    end
    print("\n")
  end
end
def main() 
  obj = MyPattern.new()
  # Test Case
  obj.show_pattern(5)
  obj.show_pattern(7)
  obj.show_pattern(3)
end
main()

Output

Size  :5 

*   *
 * * 
  *  
 * * 
*   *

Size  :7 

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

Size  :3 

* *
 * 
* *

/*
  Scala Program
  Print X pattern
*/
class MyPattern {
  def space(size: Int): Unit = {
    var i: Int = 0;
    while (i < size) {
      print(" ");
      i += 1;
    }
  }
  def show_pattern(size: Int): Unit = {
    if (size <= 0 ||
      size % 2 == 0) {
      return;
    }
    print("Size : " + size + " \n\n");

    //Variable which is manage display value
    var a: Int = 0;
    var b: Int = size - 1;
    var i: Int = 0;
    while (i < size) {
      var j: Int = 0;
      while (j < size) {
        if (j == a) {
          print("*");
        } else
        if (j == b) {
          print("*");
        } else {
          print(" ");
        }
        j += 1;
      }
      a += 1;
      b -= 1;
      print("\n");
      i += 1;
    }
    print("\n");
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();

    //Test Case
    obj.show_pattern(5);
    obj.show_pattern(7);
    obj.show_pattern(3);
  }
}

Output

Size : 5

*   *
 * *
  *
 * *
*   *

Size : 7

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

Size : 3

* *
 *
* *
/*
  Swift Program
  Print X pattern
*/
class MyPattern {
  func space(_ size: Int) {
    var i: Int = 0;
    while (i < size) {
      print(" ", terminator: "");
      i += 1;
    }
  }
  func show_pattern(_ size: Int) {
    if (size <= 0 ||
      size % 2 == 0) {
      return;
    }
    print("Size : ", size ," \n\n", terminator: "");
    //Variable which is manage display value
    var a: Int = 0;
    var b: Int = size - 1;
    var i: Int = 0;
    while (i < size) {
      var j: Int = 0;
      while (j < size) {
        if (j == a) {
          print("*", terminator: "");
        } else
        if (j == b) {
          print("*", terminator: "");
        } else {
          print(" ", terminator: "");
        }
        j += 1;
      }
      a += 1;
      b -= 1;
      print("\n", terminator: "");
      i += 1;
    }
    print("\n", terminator: "");
  }
}
func main() {
  let obj: MyPattern = MyPattern();
  //Test Case
  obj.show_pattern(5);
  obj.show_pattern(7);
  obj.show_pattern(3);
}
main();

Output

Size :  5

*   *
 * *
  *
 * *
*   *

Size :  7

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

Size :  3

* *
 *
* *
//C Program
//Print X pattern with given size
#include <stdio.h>

void space(int size)
{
  for (int i = 0; i < size; ++i)
  {
    printf(" ");
  }
}
void show_pattern(int size,int n)
{
  if(size<=0 || size %2 == 0)
  {
    //When size is Odd or less than zero
    return;
  }
  printf("Size : %d  N : %d \n\n",size,n );

  //Variable which is manage display value
  int a = 0 , b = size - 1; 

  for (int i = 0; i < size; ++i)
  {
    for(int k = 0;k<n;k++)
    {

      for (int j = 0; j < size; ++j)
      {
        if(j == a )
        {
          printf("*");

        }
        else if(j==b)
        {
          printf("*");

        }
        else
        {
          printf(" ");
        }
      }
      space(4);
    }
    a++;
    b--;
    printf("\n");
  }

  printf("\n");
}

int main()
{
  //Test Case
  show_pattern(5,4);
  show_pattern(7,3);
  show_pattern(3,4);
  
  return 0;
}

Output

Size : 5  N : 4

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

Size : 7  N : 3

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

Size : 3  N : 4

* *    * *    * *    * *
 *      *      *      *
* *    * *    * *    * *
/*
  C++ Program
  Print X pattern with given size
*/
#include<iostream>

using namespace std;
class MyPattern {
  public:
    void space(int size) {
      for (int i = 0; i < size; ++i) {
        cout << " ";
      }
    }
  void show_pattern(int size, int n) {
    if (size <= 0 ||
      size % 2 == 0) {
      return;
    }
    cout << "Size : " << size << " N : " << n << " \n\n";
    //Variable which is manage display value
    int a = 0, b = size - 1;
    for (int i = 0; i < size; ++i) {
      for (int k = 0; k < n; k++) {
        for (int j = 0; j < size; ++j) {
          if (j == a) {
            cout << "*";
          } else
          if (j == b) {
            cout << "*";
          } else {
            cout << " ";
          }
        }
        this->space(4);
      }
      a++;
      b--;
      cout << "\n";
    }
    cout << "\n";
  }
};
int main() {
  MyPattern obj =  MyPattern();
  //Test Case
  obj.show_pattern(5, 4);
  obj.show_pattern(7, 3);
  obj.show_pattern(3, 4);
  return 0;
}

Output

Size : 5 N : 4

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

Size : 7 N : 3

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

Size : 3 N : 4

* *    * *    * *    * *
 *      *      *      *
* *    * *    * *    * *
/*
  C# Program
  Print X pattern with given size
*/
using System;
public class MyPattern {
  public void space(int size) {
    for (int i = 0; i < size; ++i) {
      Console.Write(" ");
    }
  }
  public void show_pattern(int size, int n) {
    if (size <= 0 ||
      size % 2 == 0) {
      return;
    }
    Console.Write("Size : " + size + " N : " + n + " \n\n");
    //Variable which is manage display value
    int a = 0, b = size - 1;
    for (int i = 0; i < size; ++i) {
      for (int k = 0; k < n; k++) {
        for (int j = 0; j < size; ++j) {
          if (j == a) {
            Console.Write("*");
          } else
          if (j == b) {
            Console.Write("*");
          } else {
            Console.Write(" ");
          }
        }
        space(4);
      }
      a++;
      b--;
      Console.Write("\n");
    }
    Console.Write("\n");
  }
  public static void Main(String[] args) {
    MyPattern obj = new MyPattern();
    obj.show_pattern(5, 4);
    obj.show_pattern(7, 3);
    obj.show_pattern(3, 4);
  }
}

Output

Size : 5 N : 4

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

Size : 7 N : 3

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

Size : 3 N : 4

* *    * *    * *    * *
 *      *      *      *
* *    * *    * *    * *
<?php
/*
  Php Program
  Print X pattern with given size
*/
class MyPattern {
  public  function space($size) {
    for ($i = 0; $i < $size; ++$i) {
      echo(" ");
    }
  }
  public  function show_pattern($size, $n) {
    if ($size <= 0 ||
      $size % 2 == 0) {
      return;
    }
    echo("Size : ". $size ." N : ". $n ." \n\n");
    //Variable which is manage display value
    $a = 0;
    $b = $size - 1;
    for ($i = 0; $i < $size; ++$i) {
      for ($k = 0; $k < $n; $k++) {
        for ($j = 0; $j < $size; ++$j) {
          if ($j == $a) {
            echo("*");
          } else
          if ($j == $b) {
            echo("*");
          } else {
            echo(" ");
          }
        }
        $this->space(4);
      }
      $a++;
      $b--;
      echo("\n");
    }
    echo("\n");
  }
}

function main() {
  $obj = new MyPattern();
  //Test Case
  $obj->show_pattern(5, 4);
  $obj->show_pattern(7, 3);
  $obj->show_pattern(3, 4);

}
main();

Output

Size : 5 N : 4

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

Size : 7 N : 3

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

Size : 3 N : 4

* *    * *    * *    * *
 *      *      *      *
* *    * *    * *    * *
/*
  Node Js Program
  Print X pattern with given size
*/
class MyPattern {
  space(size) {
    for (var i = 0; i < size; ++i) {
      process.stdout.write(" ");
    }
  }
  show_pattern(size, n) {
    if (size <= 0 ||
      size % 2 == 0) {
      return;
    }

    process.stdout.write("Size : " + size + " N : " + n + " \n\n");
    //Variable which is manage display value
    var a = 0;
    var b = size - 1;
    for (var i = 0; i < size; ++i) {
      for (var k = 0; k < n; k++) {
        for (var j = 0; j < size; ++j) {
          if (j == a) {
            process.stdout.write("*");
          } else
          if (j == b) {
            process.stdout.write("*");
          } else {
            process.stdout.write(" ");
          }
        }
        this.space(4);
      }
      a++;
      b--;
      process.stdout.write("\n");
    }

    process.stdout.write("\n");
  }
}

function main(args) {
  var obj = new MyPattern();
  //Test Case
  obj.show_pattern(5, 4);
  obj.show_pattern(7, 3);
  obj.show_pattern(3, 4);
}

main();

Output

Size : 5 N : 4

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

Size : 7 N : 3

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

Size : 3 N : 4

* *    * *    * *    * *
 *      *      *      *
* *    * *    * *    * *
#   Python 3 Program
#   Print X pattern with given size

class MyPattern :
  def space(self, size) :
    i = 0
    while (i < size) :
      print(" ", end = "")
      i += 1
    
  
  def show_pattern(self, size, n) :
    if (size <= 0 or size % 2 == 0) :
      return
    
    print("Size : ", size ," N : ", n ," \n\n", end = "")
    # Variable which is manage display value
    a = 0
    b = size - 1
    i = 0
    j = 0
    k = 0
    while (i < size) :
      k = 0
      while (k < n) :
        j = 0
        while (j < size) :
          if (j == a) :
            print("*", end = "")
          elif (j == b) :
            print("*", end = "")
          else :
            print(" ", end = "")
          
          j += 1
        
        self.space(4)
        k += 1
      
      a += 1
      b -= 1
      print("\n", end = "")
      i += 1
    
    print("\n", end = "")
  

def main() :
  obj = MyPattern()
  # Test Case
  obj.show_pattern(5, 4)
  obj.show_pattern(7, 3)
  obj.show_pattern(3, 4)


if __name__ == "__main__":
  main()

Output

Size :  5  N :  4

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

Size :  7  N :  3

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

Size :  3  N :  4

* *    * *    * *    * *
 *      *      *      *
* *    * *    * *    * *
#   Ruby Program
#   Print X pattern with given size

class MyPattern 
  def space(size) 
    i = 0
    while (i < size) 
      print(" ")
      i += 1
    end
  end
  def show_pattern(size, n) 
    if (size <= 0 ||
      size % 2 == 0) 
      return
    end
    print("Size  :", size ," N  :", n ," \n\n")
    # Variable which is manage display value
    a = 0
    b = size - 1
    i = 0
    j = 0
    k = 0
    while (i < size) 
      k = 0
      while (k < n) 
        j = 0
        while (j < size) 
          if (j == a) 
            print("*")
          elsif (j == b) 
            print("*")
          else 
            print(" ")
          end
          j += 1
        end
        self.space(4)
        k += 1
      end
      a += 1
      b -= 1
      print("\n")
      i += 1
    end
    print("\n")
  end
end
def main() 
  obj = MyPattern.new()
  # Test Case
  obj.show_pattern(5, 4)
  obj.show_pattern(7, 3)
  obj.show_pattern(3, 4)
end
main()

Output

Size  :5 N  :4 

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

Size  :7 N  :3 

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

Size  :3 N  :4 

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

/*
  Scala Program
  Print X pattern with given size
*/
class MyPattern {
  def space(size: Int): Unit = {
    var i: Int = 0;
    while (i < size) {
      print(" ");
      i += 1;
    }
  }
  def show_pattern(size: Int, n: Int): Unit = {
    if (size <= 0 ||
      size % 2 == 0) {
      return;
    }
    print("Size : " + size + " N : " + n + " \n\n");

    //Variable which is manage display value
    var a: Int = 0;
    var b: Int = size - 1;
    var i: Int = 0;
    var j: Int = 0;
    var k: Int = 0;
    while (i < size) {
      k = 0;
      while (k < n) {
        j = 0;
        while (j < size) {
          if (j == a) {
            print("*");
          } else
          if (j == b) {
            print("*");
          } else {
            print(" ");
          }
          j += 1;
        }
        space(4);
        k += 1;
      }
      a += 1;
      b -= 1;
      print("\n");
      i += 1;
    }
    print("\n");
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();

    //Test Case
    obj.show_pattern(5, 4);
    obj.show_pattern(7, 3);
    obj.show_pattern(3, 4);
  }
}

Output

Size : 5 N : 4

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

Size : 7 N : 3

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

Size : 3 N : 4

* *    * *    * *    * *
 *      *      *      *
* *    * *    * *    * *
/*
  Swift Program
  Print X pattern with given size
*/
class MyPattern {
  func space(_ size: Int) {
    var i: Int = 0;
    while (i < size) {
      print(" ", terminator: "");
      i += 1;
    }
  }
  func show_pattern(_ size: Int, _ n: Int) {
    if (size <= 0 ||
      size % 2 == 0) {
      return;
    }
    print("Size : ", size ," N : ", n ," \n\n", terminator: "");
    //Variable which is manage display value
    var a: Int = 0;
    var b: Int = size - 1;
    var i: Int = 0;
    var j: Int = 0;
    var k: Int = 0;
    while (i < size) {
      k = 0;
      while (k < n) {
        j = 0;
        while (j < size) {
          if (j == a) {
            print("*", terminator: "");
          } else
          if (j == b) {
            print("*", terminator: "");
          } else {
            print(" ", terminator: "");
          }
          j += 1;
        }
        self.space(4);
        k += 1;
      }
      a += 1;
      b -= 1;
      print("\n", terminator: "");
      i += 1;
    }
    print("\n", terminator: "");
  }
}
func main() {
  let obj: MyPattern = MyPattern();
  //Test Case
  obj.show_pattern(5, 4);
  obj.show_pattern(7, 3);
  obj.show_pattern(3, 4);
}
main();

Output

Size :  5  N :  4

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

Size :  7  N :  3

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

Size :  3  N :  4

* *    * *    * *    * *
 *      *      *      *
* *    * *    * *    * *
/*
  Java Program
  Print X pattern with given size
*/

public class MyPattern {

  public void space(int size) 
  {
    for (int i = 0; i < size; ++i) 
    {
      System.out.print(" ");
    }
  }
  public void show_pattern(int size,int n) 
  {
    if (size <= 0 || size%2 == 0) 
    {
      return;
    }
    System.out.print("Size : "+size+" N : "+n+" \n\n");
    //Variable which is manage display value
    int a = 0, b = size - 1;
    for (int i = 0; i < size; ++i) 
    {
      for(int k = 0;k<n;k++)
      {
        for (int j = 0; j < size; ++j) 
        {
          if (j == a) 
          {
            System.out.print("*");
          } 
          else if (j == b) 
          {
            System.out.print("*");
          } 
          else 
          {
            System.out.print(" ");
          }
        }
        space(4);
      }

      a++;
      b--;
      System.out.print("\n");
    }
    System.out.print("\n");
  }
  public static void main(String[] args) {

    MyPattern obj = new MyPattern();
    //Test Case
    obj.show_pattern(5,4);
    obj.show_pattern(7,3);
    obj.show_pattern(3,4);
  

  }
}

Output

Size :  5  N :  4

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

Size :  7  N :  3

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

Size :  3  N :  4

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


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