Posted on by Kalkicode
Code Pattern

Print X patterns

In this article, we will discuss how to print X patterns using a C program. The program will take an input size and generate an X pattern using asterisks and spaces.

Problem Statement

The problem is to write a program that prints an X pattern of a given size. The size should be an odd positive integer. The program should output the X pattern using asterisks (*) and spaces.

For example, if the input size is 5, the program should output the following pattern:

*   *
 * *
  *
 * *
*   *

Explanation with an Example

Let's understand how the pattern is formed using the example of a size 5 pattern.

First, we initialize two variables, a and b, as 0 and size - 1 respectively. These variables will keep track of the positions where the asterisks need to be placed.

We then iterate over each row and column of the pattern. For each position, if the column index matches a or b, we print an asterisk. Otherwise, we print a space.

After printing each row, we increment a and decrement b by 1. This helps in moving towards the center of the pattern.

Finally, we repeat this process for all rows, resulting in the complete X pattern.

Algorithm and Pseudocode

Let's outline the algorithm to print the X pattern:

  1. Check if the given size is valid. If it is even or less than or equal to 0, return.
  2. Print the size of the pattern.
  3. Initialize variables a and b as 0 and size - 1 respectively.
  4. Iterate over each row from 0 to size - 1.
    1. Iterate over each column from 0 to size - 1.
      1. If the column index is equal to a, print an asterisk.
      2. Else if the column index is equal to b, print an asterisk.
      3. Otherwise, print a space.
    2. Increment a by 1 and decrement b by 1.

Here is the pseudocode for the above algorithm:

function space(size):
    for i from 0 to size - 1:
        print a space

function show_pattern(size):
    if size <= 0 or size % 2 == 0:
        return
    print "Size: ", size
    a = 0
    b = size - 1
    for i from 0 to size - 1:
        for j from 0 to size - 1:
            if j == a:
                print an asterisk
            else if j == b:
                print an asterisk
            else:
                print a space
        increment a by 1
        decrement b by 1

main:
    show_pattern(5)
    show_pattern(7)
    show_pattern(3)

Code Soliton

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

* *
 *
* *

Resultant Output Explanation

When we run the provided C program, it generates the following output:

Size: 5
*   *
 * *
  *
 * *
*   *
Size: 7
*     *
 *   *
  * *
   *
  * *
 *   *
*     *
Size: 3
* *
 *
* *

The output shows the X patterns for different input sizes. The asterisks (*) represent the diagonal lines of the X pattern, while the spaces represent the empty areas.

Time Complexity

The time complexity of this program is O(n^2), where n is the size of the pattern. This is because we have nested loops that iterate over each row and column of the pattern, resulting in a quadratic time complexity.

As the input size increases, the number of iterations required also increases quadratically. Therefore, it is important to note that the program's efficiency may decrease for very large input sizes.

Another complicated X pattern

This is similar to above but its side by side.

//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

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

Comment

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