Posted on by Kalkicode
Code Pattern

Print Z pattern

In this article, we will discuss how to print a Z pattern using a C program. We'll explain the problem statement, provide an explanation with suitable examples, present the algorithm and pseudocode, and finally, explain the output with time complexity of the code.

Problem Statement

The problem is to write a program that prints a Z pattern of asterisks based on the given size. The size represents the height of the Z pattern, and each line of the pattern consists of asterisks (*) and spaces ( ).

Explanation with Examples

Let's understand the problem with some examples. Consider the following:

Example 1:

Size: 5

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

Example 2:

Size: 7

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

Example 3:

Size: 3

* * *
  *
* * *

Example 4:

Size: 9

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

In the examples above, we can observe that the Z pattern is formed using asterisks. The first and last lines contain a row of asterisks equal to the size minus one. The middle lines contain spaces to align the subsequent asterisk.

Algorithm and Pseudocode

To print the Z pattern, we can follow the following algorithm:

  1. Define a function to display spaces. This function takes an integer size as input.
  2. Inside the space() function, iterate from 0 to size and print a space character.
  3. Define a function to display asterisks. This function takes an integer size as input.
  4. Inside the display_star() function, iterate from 0 to size and print an asterisk followed by a space.
  5. Define the main function.
  6. Inside the main function, define a function called show_z() that takes an integer size as input.
  7. If the size is less than 2, return from the show_z() function.
  8. Print the value of size.
  9. Initialize a variable called side with the value of ((size-1)*2).
  10. Use a loop to print the Z pattern:
    1. If the current iteration is the first or last line, call the display_star() function with size-1 as the argument.
    2. Otherwise, call the space() function with the side value as the argument.
    3. Print an asterisk (*) on each line.
    4. Decrease the value of side by 2 in each iteration.
  11. Print a newline character.
  12. In the main function, call the show_z() function with different test cases to demonstrate the pattern.
  13. Return 0 to indicate successful execution of the program.

The pseudocode for the given algorithm can be written as follows:

function space(size):
    for i = 0 to size:
        print " "
        
function display_star(size):
    for i = 0 to size:
        print "* "

function show_z(size):
    if size < 2:
        return
    print "Size: " + size + "\n\n"
    side = (size-1)*2
    for i = 0 to size:
        if i == 0 or i == size-1:
            display_star(size-1)
        else:
            space(side)
        print "*"
        print "\n"
        side = side - 2

main:
    show_z(5)
    show_z(7)
    show_z(3)
    show_z(9)
    return 0

Code Solution

// C Program
// Display Z pattern 
#include <stdio.h>

//include space 
void space(int size)
{
  for (int i = 0; i < size; ++i)
  {
    printf(" ");
  }
}
//include star 
void display_star(int size)
{
  for (int i = 0; i < size; i++)
  {
    printf("* ");
  }
}
void show_z(int size)
{
  if(size < 2 )
  {
    return;
  }
  printf("Size : %d\n\n",size );

  int side = ((size-1)*2);

  //Display Z pattern of given size
  for (int i = 0; i < size; i++)
  {
    if(i == 0 || i == size-1)
    {
      display_star(size-1);
    }
    else
    {
      space(side);
    }
    printf("*");
    printf("\n");
    side-=2;

  }
 
  printf("\n");
}

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

Output

Size : 5

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

Size : 7

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

Size : 3

* * *
  *
* * *

Size : 9

* * * * * * * * *
              *
            *
          *
        *
      *
    *
  *
* * * * * * * * *
// C++ Program
// Display Z pattern 
#include<iostream>

using namespace std;
class MyPattern {
  public:

    //include space 
    void space(int size) {
      for (int i = 0; i < size; ++i) {
        cout << " ";
      }
    }
  //include star 
  void display_star(int size) {
    for (int i = 0; i < size; i++) {
      cout << "* ";
    }
  }
  void show_z(int size) {
    if (size < 2) {
      return;
    }
    cout << "Size : " << size << "\n\n";
    int side = ((size - 1) *2);
    //Display Z pattern of given size

    for (int i = 0; i < size; i++) {
      if (i == 0 ||
        i == size - 1) {
        this->display_star(size - 1);
      } else {
        this->space(side);
      }
      cout << "*";
      cout << "\n";
      side -= 2;
    }
    cout << "\n";
  }
};
int main() {
  MyPattern obj =  MyPattern();
  //Test Case
  obj.show_z(6);
  obj.show_z(3);
  obj.show_z(4);
  obj.show_z(9);
  return 0;
}

Output

Size : 6

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

Size : 3

* * *
  *
* * *

Size : 4

* * * *
    *
  *
* * * *

Size : 9

* * * * * * * * *
              *
            *
          *
        *
      *
    *
  *
* * * * * * * * *
// Java Program
// Display Z pattern 
class MyPattern {

  //include space 
  public void space(int size) {
    for (int i = 0; i < size; ++i) {
      System.out.print(" ");
    }
  }
  //include star 
  public void display_star(int size) {
    for (int i = 0; i < size; i++) {
      System.out.print("* ");
    }
  }
  public void show_z(int size) {
    if (size < 2) {
      return;
    }
    System.out.print("Size : "+size+"\n\n");
    int side = ((size - 1) * 2);
    //Display Z pattern of given size

    for (int i = 0; i < size; i++) {
      if (i == 0 || i == size - 1) {
        display_star(size - 1);
      } else {
        space(side);
      }
      System.out.print("*");
      System.out.print("\n");
      side -= 2;
    }
    System.out.print("\n");
  }
  public static void main(String[] args) 
  {
    MyPattern obj = new MyPattern();
    //Test Case
    obj.show_z(6);
    obj.show_z(3);
    obj.show_z(4);
    obj.show_z(9);
  }
}

Output

Size : 6

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

Size : 3

* * *
  *
* * *

Size : 4

* * * *
    *
  *
* * * *

Size : 9

* * * * * * * * *
              *
            *
          *
        *
      *
    *
  *
* * * * * * * * *
// C# Program
// Display Z pattern 
using System;
public class MyPattern {
  //include space 
  public void space(int size) {
    for (int i = 0; i < size; ++i) {
      Console.Write(" ");
    }
  }
  //include star 
  public void display_star(int size) {
    for (int i = 0; i < size; i++) {
      Console.Write("* ");
    }
  }
  public void show_z(int size) {
    if (size < 2) {
      return;
    }
    Console.Write("Size : " + size + "\n\n");
    int side = ((size - 1) * 2);
    //Display Z pattern of given size

    for (int i = 0; i < size; i++) {
      if (i == 0 ||
        i == size - 1) {
        display_star(size - 1);
      } else {
        space(side);
      }
      Console.Write("*");
      Console.Write("\n");
      side -= 2;
    }
    Console.Write("\n");
  }
  public static void Main(String[] args) {
    MyPattern obj = new MyPattern();
    obj.show_z(6);
    obj.show_z(3);
    obj.show_z(4);
    obj.show_z(9);
  }
}

Output

Size : 6

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

Size : 3

* * *
  *
* * *

Size : 4

* * * *
    *
  *
* * * *

Size : 9

* * * * * * * * *
              *
            *
          *
        *
      *
    *
  *
* * * * * * * * *
<?php
// Php Program
// Display Z pattern 
class MyPattern {
  //include space 
  public  function space($size) {
    for ($i = 0; $i < $size; ++$i) {
      echo(" ");
    }
  }
  //include star 

  public  function display_star($size) {
    for ($i = 0; $i < $size; $i++) {
      echo("* ");
    }
  }
  public  function show_z($size) {
    if ($size < 2) {
      return;
    }
    echo("Size : ". $size ."\n\n");
    $side = (($size - 1) *2);
    //Display Z pattern of given size

    for ($i = 0; $i < $size; $i++) {
      if ($i == 0 ||
        $i == $size - 1) {
        $this->display_star($size - 1);
      } else {
        $this->space($side);
      }
      echo("*");
      echo("\n");
      $side -= 2;
    }
    echo("\n");
  }
}

function main() {
  $obj = new MyPattern();
  //Test Case
  $obj->show_z(6);
  $obj->show_z(3);
  $obj->show_z(4);
  $obj->show_z(9);

}
main();

Output

Size : 6

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

Size : 3

* * *
  *
* * *

Size : 4

* * * *
    *
  *
* * * *

Size : 9

* * * * * * * * *
              *
            *
          *
        *
      *
    *
  *
* * * * * * * * *
// Node Js Program
// Display Z pattern 
class MyPattern {
  //include space 
  space(size) {
    for (var i = 0; i < size; ++i) {
      process.stdout.write(" ");
    }
  }

  //include star 
  display_star(size) {
    for (var i = 0; i < size; i++) {
      process.stdout.write("* ");
    }
  }
  show_z(size) {
    if (size < 2) {
      return;
    }

    process.stdout.write("Size : " + size + "\n\n");
    var side = ((size - 1) *2);
    //Display Z pattern of given size

    for (var i = 0; i < size; i++) {
      if (i == 0 ||
        i == size - 1) {
        this.display_star(size - 1);
      } else {
        this.space(side);
      }

      process.stdout.write("*");
      process.stdout.write("\n");
      side -= 2;
    }

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

function main(args) {
  var obj = new MyPattern();
  //Test Case
  obj.show_z(6);
  obj.show_z(3);
  obj.show_z(4);
  obj.show_z(9);
}

main();

Output

Size : 6

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

Size : 3

* * *
  *
* * *

Size : 4

* * * *
    *
  *
* * * *

Size : 9

* * * * * * * * *
              *
            *
          *
        *
      *
    *
  *
* * * * * * * * *
#  Python 3 Program
#  Display Z pattern 
class MyPattern :
  # include space 
  def space(self, size) :
    i = 0
    while (i < size) :
      print(" ", end = "")
      i += 1
    
  
  # include star 
  def display_star(self, size) :
    i = 0
    while (i < size) :
      print("*", end = " ")
      i += 1
    
  
  def show_z(self, size) :
    if (size < 2) :
      return
    
    print("Size : ", size ,"\n\n", end = "")
    side = ((size - 1) * 2)
    i = 0
    # Display Z pattern of given size
    while (i < size) :
      if (i == 0 or i == size - 1) :
        self.display_star(size - 1)
      else :
        self.space(side)
      
      print("*", end = "")
      print("\n", end = "")
      side -= 2
      i += 1
    
    print("\n", end = "")
  

def main() :
  obj = MyPattern()
  obj.show_z(6)
  obj.show_z(3)
  obj.show_z(4)
  obj.show_z(9)


if __name__ == "__main__":
  main()

Output

Size :  6

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

Size :  3

* * *
  *
* * *

Size :  4

* * * *
    *
  *
* * * *

Size :  9

* * * * * * * * *
              *
            *
          *
        *
      *
    *
  *
* * * * * * * * *
#  Ruby Program
#  Display Z pattern 
class MyPattern 
  # include space 
  def space(size) 
    i = 0
    while (i < size) 
      print(" ")
      i += 1
    end
  end
  # include star 
  def display_star(size) 
    i = 0
    while (i < size) 
      print("* ")
      i += 1
    end
  end
  def show_z(size) 
    if (size < 2) 
      return
    end
    print("Size  : ", size ,"\n\n")
    side = ((size - 1) * 2)
    i = 0
    # Display Z pattern of given size
    while (i < size) 
      if (i == 0 ||
        i == size - 1) 
        self.display_star(size - 1)
      else 
        self.space(side)
      end
      print("*")
      print("\n")
      side -= 2
      i += 1
    end
    print("\n")
  end
end
def main() 
  obj = MyPattern.new()
  obj.show_z(6)
  obj.show_z(3)
  obj.show_z(4)
  obj.show_z(9)
end
main()

Output

Size  : 6

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

Size  : 3

* * *
  *
* * *

Size  : 4

* * * *
    *
  *
* * * *

Size  : 9

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

// Scala Program
// Display Z pattern 
class MyPattern {
  //include space 
  def space(size: Int): Unit = {
    var i: Int = 0;
    while (i < size) {
      print(" ");
      i += 1;
    }
  }
  //include star 
  def display_star(size: Int): Unit = {
    var i: Int = 0;
    while (i < size) {
      print("* ");
      i += 1;
    }
  }
  def show_z(size: Int): Unit = {
    if (size < 2) {
      return;
    }
    print("Size : " + size + "\n\n");
    var side: Int = ((size - 1) * 2);
    var i: Int = 0;

    //Display Z pattern of given size
    while (i < size) {
      if (i == 0 ||
        i == size - 1) {
        display_star(size - 1);
      } else {
        space(side);
      }
      print("*");
      print("\n");
      side -= 2;
      i += 1;
    }
    print("\n");
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();
    obj.show_z(6);
    obj.show_z(3);
    obj.show_z(4);
    obj.show_z(9);
  }
}

Output

Size : 6

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

Size : 3

* * *
  *
* * *

Size : 4

* * * *
    *
  *
* * * *

Size : 9

* * * * * * * * *
              *
            *
          *
        *
      *
    *
  *
* * * * * * * * *
// Swift Program
// Display Z pattern 
class MyPattern {
  //include space 
  func space(_ size: Int) {
    var i: Int = 0;
    while (i < size) {
      print(" ", terminator: "");
      i += 1;
    }
  }
  //include star 
  func display_star(_ size: Int) {
    var i: Int = 0;
    while (i < size) {
      print("*", terminator: " ");
      i += 1;
    }
  }
  func show_z(_ size: Int) {
    if (size < 2) {
      return;
    }
    print("Size : ", size ,"\n\n", terminator: "");
    var side: Int = ((size - 1) * 2);
    var i: Int = 0;
    //Display Z pattern of given size
    while (i < size) {
      if (i == 0 ||
        i == size - 1) {
        self.display_star(size - 1);
      } else {
        self.space(side);
      }
      print("*", terminator: "");
      print(terminator: "\n");
      side -= 2;
      i += 1;
    }
    print(terminator: "\n");
  }
}
func main() {
  let obj: MyPattern = MyPattern();
  obj.show_z(6);
  obj.show_z(3);
  obj.show_z(4);
  obj.show_z(9);
}
main();

Output

Size :  6

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

Size :  3

* * *
  *
* * *

Size :  4

* * * *
    *
  *
* * * *

Size :  9

* * * * * * * * *
              *
            *
          *
        *
      *
    *
  *
* * * * * * * * *
// Rust Program
// Display Z pattern 
fn main() {
  //Test Case
  show_z(5);
  show_z(7);
  show_z(3);
  show_z(9);
}
fn show_z(size: i32) {
  if size < 2 {
    return;
  }
  print!("Size : {}\n\n", size);
  let mut side: i32 = (size - 1) * 2;
  let mut i: i32 = 0;
  //Display Z pattern of given size

  while i < size {
    if i == 0 || i == size - 1 {
      display_star(size - 1);
    }
    else {
      space(side);
    }
    print!("*");
    print!("\n");
    side -= 2;
    i += 1;
  }
  print!("\n");
}
fn display_star(size: i32) {
  let mut i: i32 = 0;
  while i < size {
    print!("* ");
    i += 1;
  }
}
fn space(size: i32) {
  let mut i: i32 = 0;
  while i < size {
    print!(" ");
    i += 1;
  }
}

Output

Size : 5

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

Size : 7

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

Size : 3

* * *
  *
* * *

Size : 9

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

The output matches the expected Z pattern for each test case. The pattern is printed based on the given size, and the asterisks (*) are arranged in the shape of a Z.

Time Complexity

The time complexity of the code is O(size), where size is the input parameter representing the height of the Z pattern. The code iterates over the size to print each line of the pattern. Therefore, the time complexity is linearly proportional to the size.

Inverted Z pattern

// C Program
// Display inverted Z pattern 
#include <stdio.h>

//include space 
void space(int size)
{
  for (int i = 0; i < size; ++i)
  {
    printf(" ");
  }
}
//include star 
void display_star(int size)
{
  for (int i = 0; i < size; i++)
  {
    printf("* ");
  }
}
//Display inverted Z pattern of given size
void print_inverted_z(int size)
{
  if (size <= 2)
  {
    return;
  }
  printf("Size : %d\n\n", size);
  //Display Z pattern of given size
  for (int i = 0; i < size; i++)
  {
    if (i == 0 || i == size - 1)
    {
      display_star(size - 1);
    }
    else
    {
      space(i + i);
    }
    printf("*");
    printf("\n");
  }
  printf("\n");
}
int main()
{
  //Test Cases
  print_inverted_z(5);
  print_inverted_z(7);
  print_inverted_z(3);
  print_inverted_z(9);
  print_inverted_z(6);
  return 0;
}

Output

Size : 5

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

Size : 7

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

Size : 3

* * *
  *
* * *

Size : 9

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

Size : 6

* * * * * *
  *
    *
      *
        *
* * * * * *
// Java Program
// Display inverted Z pattern 
class MyPattern
{
  //include space 
  public void space(int size)
  {
    for (int i = 0; i < size; ++i)
    {
      System.out.print(" ");
    }
  }
  //include star 
  public void display_star(int size)
  {
    for (int i = 0; i < size; i++)
    {
      System.out.print("* ");
    }
  }
  //Display inverted Z pattern of given size
  public void print_inverted_z(int size)
  {
    if (size <= 2)
    {
      return;
    }
    System.out.print("Size : " + size + "\n\n");
    //Display Z pattern of given size
    for (int i = 0; i < size; i++)
    {
      if (i == 0 || i == size - 1)
      {
        display_star(size - 1);
      }
      else
      {
        space(i + i);
      }
      System.out.print("*");
      System.out.print("\n");
    }
    System.out.print("\n");
  }
  public static void main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Test Cases
    obj.print_inverted_z(5);
    obj.print_inverted_z(7);
    obj.print_inverted_z(3);
    obj.print_inverted_z(9);
    obj.print_inverted_z(6);
  }
}

Output

Size : 5

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

Size : 7

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

Size : 3

* * *
  *
* * *

Size : 9

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

Size : 6

* * * * * *
  *
    *
      *
        *
* * * * * *
// C++ Program
// Display inverted Z pattern 
#include<iostream>

using namespace std;
class MyPattern
{
  public:
    //include space 
    void space(int size)
    {
      for (int i = 0; i < size; ++i)
      {
        cout << " ";
      }
    }
  //include star 
  void display_star(int size)
  {
    for (int i = 0; i < size; i++)
    {
      cout << "* ";
    }
  }
  //Display inverted Z pattern of given size
  void print_inverted_z(int size)
  {
    if (size <= 2)
    {
      return;
    }
    cout << "Size : " << size << "\n\n";
    //Display Z pattern of given size
    for (int i = 0; i < size; i++)
    {
      if (i == 0 || i == size - 1)
      {
        this->display_star(size - 1);
      }
      else
      {
        this->space(i + i);
      }
      cout << "*";
      cout << "\n";
    }
    cout << "\n";
  }
};
int main()
{
  MyPattern obj;
  //Test Cases
  obj.print_inverted_z(5);
  obj.print_inverted_z(7);
  obj.print_inverted_z(3);
  obj.print_inverted_z(9);
  obj.print_inverted_z(6);
  return 0;
}

Output

Size : 5

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

Size : 7

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

Size : 3

* * *
  *
* * *

Size : 9

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

Size : 6

* * * * * *
  *
    *
      *
        *
* * * * * *
// C# Program
// Display inverted Z pattern 
using System;
class MyPattern
{
  //include space 
  public void space(int size)
  {
    for (int i = 0; i < size; i++)
    {
      Console.Write(" ");
    }
  }
  //include star 
  public void display_star(int size)
  {
    for (int i = 0; i < size; i++)
    {
      Console.Write("* ");
    }
  }
  //Display inverted Z pattern of given size
  public void print_inverted_z(int size)
  {
    if (size <= 2)
    {
      return;
    }
    Console.Write("Size : " + size + "\n\n");
    //Display Z pattern of given size
    for (int i = 0; i < size; i++)
    {
      if (i == 0 || i == size - 1)
      {
        display_star(size - 1);
      }
      else
      {
        space(i + i);
      }
      Console.Write("*");
      Console.Write("\n");
    }
    Console.Write("\n");
  }
  public static void Main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Test Cases
    obj.print_inverted_z(5);
    obj.print_inverted_z(7);
    obj.print_inverted_z(3);
    obj.print_inverted_z(9);
    obj.print_inverted_z(6);
  }
}

Output

Size : 5

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

Size : 7

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

Size : 3

* * *
  *
* * *

Size : 9

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

Size : 6

* * * * * *
  *
    *
      *
        *
* * * * * *
<?php
// Php Program
// Display inverted Z pattern 
class MyPattern
{
  //include space 
  public  function space($size)
  {
    for ($i = 0; $i < $size; ++$i)
    {
      echo(" ");
    }
  }
  //include star 
  public  function display_star($size)
  {
    for ($i = 0; $i < $size; $i++)
    {
      echo("* ");
    }
  }
  //Display inverted Z pattern of given size
  public  function print_inverted_z($size)
  {
    if ($size <= 2)
    {
      return;
    }
    echo("Size : ". $size ."\n\n");
    //Display Z pattern of given size
    for ($i = 0; $i < $size; $i++)
    {
      if ($i == 0 || $i == $size - 1)
      {
        $this->display_star($size - 1);
      }
      else
      {
        $this->space($i + $i);
      }
      echo("*");
      echo("\n");
    }
    echo("\n");
  }
}

function main()
{
  $obj = new MyPattern();
  //Test Cases
  $obj->print_inverted_z(5);
  $obj->print_inverted_z(7);
  $obj->print_inverted_z(3);
  $obj->print_inverted_z(9);
  $obj->print_inverted_z(6);
}
main();

Output

Size : 5

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

Size : 7

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

Size : 3

* * *
  *
* * *

Size : 9

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

Size : 6

* * * * * *
  *
    *
      *
        *
* * * * * *
// Node Js Program
// Display inverted Z pattern 
class MyPattern
{
  //include space 
  space(size)
  {
    for (var i = 0; i < size; ++i)
    {
      process.stdout.write(" ");
    }
  }
  //include star 
  display_star(size)
  {
    for (var i = 0; i < size; i++)
    {
      process.stdout.write("* ");
    }
  }
  //Display inverted Z pattern of given size
  print_inverted_z(size)
  {
    if (size <= 2)
    {
      return;
    }
    process.stdout.write("Size : " + size + "\n\n");
    //Display Z pattern of given size
    for (var i = 0; i < size; i++)
    {
      if (i == 0 || i == size - 1)
      {
        this.display_star(size - 1);
      }
      else
      {
        this.space(i + i);
      }
      process.stdout.write("*");
      process.stdout.write("\n");
    }
    process.stdout.write("\n");
  }
}

function main(args)
{
  var obj = new MyPattern();
  //Test Cases
  obj.print_inverted_z(5);
  obj.print_inverted_z(7);
  obj.print_inverted_z(3);
  obj.print_inverted_z(9);
  obj.print_inverted_z(6);
}
main();

Output

Size : 5

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

Size : 7

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

Size : 3

* * *
  *
* * *

Size : 9

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

Size : 6

* * * * * *
  *
    *
      *
        *
* * * * * *
#  Python 3 Program
#  Display inverted Z pattern 
class MyPattern :
  # include space 
  def space(self, size) :
    i = 0
    while (i < size) :
      print(" ", end = "")
      i += 1
    
  
  # include star 
  def display_star(self, size) :
    i = 0
    while (i < size) :
      print("* ", end = "")
      i += 1
    
  
  # Display inverted Z pattern of given size
  def print_inverted_z(self, size) :
    if (size <= 2) :
      return
    
    print("Size : ", size ,"\n")
    i = 0
    while (i < size) :
      if (i == 0 or i == size - 1) :
        self.display_star(size - 1)
      else :
        self.space(i + i)
      
      print("*", end = "")
      print(end = "\n")
      i += 1
    
    print(end = "\n")
  

def main() :
  obj = MyPattern()
  # Test Cases
  obj.print_inverted_z(5)
  obj.print_inverted_z(7)
  obj.print_inverted_z(3)
  obj.print_inverted_z(9)
  obj.print_inverted_z(6)


if __name__ == "__main__": main()

Output

Size :  5

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

Size :  7

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

Size :  3

* * *
  *
* * *

Size :  9

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

Size :  6

* * * * * *
  *
    *
      *
        *
* * * * * *
#  Ruby Program
#  Display inverted Z pattern 
class MyPattern

  # include space 
  def space(size)
  
    i = 0
    while (i < size)
    
      print(" ")
      i += 1
    end
  end
  # include star 
  def display_star(size)
  
    i = 0
    while (i < size)
    
      print("* ")
      i += 1
    end
  end
  # Display inverted Z pattern of given size
  def print_inverted_z(size)
  
    if (size <= 2)
    
      return
    end
    print("Size  : ", size ,"\n\n")
    i = 0
    while (i < size)
    
      if (i == 0 || i == size - 1)
      
        self.display_star(size - 1)
      else
      
        self.space(i + i)
      end
      print("*")
      print("\n")
      i += 1
    end
    print("\n")
  end
end
def main()

  obj = MyPattern.new()
  # Test Cases
  obj.print_inverted_z(5)
  obj.print_inverted_z(7)
  obj.print_inverted_z(3)
  obj.print_inverted_z(9)
  obj.print_inverted_z(6)
end
main()

Output

Size  : 5

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

Size  : 7

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

Size  : 3

* * *
  *
* * *

Size  : 9

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

Size  : 6

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

// Scala Program
// Display inverted Z pattern 
class MyPattern
{
  //include space 
  def space(size: Int): Unit = {
    var i: Int = 0;
    while (i < size)
    {
      print(" ");
      i += 1;
    }
  }
  //include star 
  def display_star(size: Int): Unit = {
    var i: Int = 0;
    while (i < size)
    {
      print("* ");
      i += 1;
    }
  }
  //Display inverted Z pattern of given size
  def print_inverted_z(size: Int): Unit = {
    if (size <= 2)
    {
      return;
    }
    print("Size : " + size + "\n\n");
    var i: Int = 0;
    while (i < size)
    {
      if (i == 0 || i == size - 1)
      {
        display_star(size - 1);
      }
      else
      {
        space(i + i);
      }
      print("*");
      print("\n");
      i += 1;
    }
    print("\n");
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();
    //Test Cases
    obj.print_inverted_z(5);
    obj.print_inverted_z(7);
    obj.print_inverted_z(3);
    obj.print_inverted_z(9);
    obj.print_inverted_z(6);
  }
}

Output

Size : 5

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

Size : 7

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

Size : 3

* * *
  *
* * *

Size : 9

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

Size : 6

* * * * * *
  *
    *
      *
        *
* * * * * *
// Swift Program
// Display inverted Z pattern 
class MyPattern
{
  //include space 
  func space(_ size: Int)
  {
    var i: Int = 0;
    while (i < size)
    {
      print(" ", terminator: "");
      i += 1;
    }
  }
  //include star 
  func display_star(_ size: Int)
  {
    var i: Int = 0;
    while (i < size)
    {
      print("* ", terminator: "");
      i += 1;
    }
  }
  //Display inverted Z pattern of given size
  func print_inverted_z(_ size: Int)
  {
    if (size <= 2)
    {
      return;
    }
    print("Size : ", size ,"\n");
    var i: Int = 0;
    while (i < size)
    {
      if (i == 0 || i == size - 1)
      {
        self.display_star(size - 1);
      }
      else
      {
        self.space(i + i);
      }
      print("*", terminator: "");
      print(terminator: "\n");
      i += 1;
    }
    print(terminator: "\n");
  }
}
func main()
{
  let obj: MyPattern = MyPattern();
  //Test Cases
  obj.print_inverted_z(5);
  obj.print_inverted_z(7);
  obj.print_inverted_z(3);
  obj.print_inverted_z(9);
  obj.print_inverted_z(6);
}
main();

Output

Size :  5

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

Size :  7

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

Size :  3

* * *
  *
* * *

Size :  9

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

Size :  6

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

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