Print Z pattern

Here given code implementation process.

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

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

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


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