Skip to main content

Print G pattern

Here given code implementation process.

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

//Include star of given size
void print_star(int size)
{
  int counter = 0;
  for (counter = 0; counter < size; counter++)
  {
    if (counter % 2 == 0)
    {
      //Add Star
      printf("*");
    }
    else
    {
      //Add Space
      printf(" ");
    }
  }
}
//Display space of given size
void print_space(int size)
{
  int counter = 0;
  for (counter = 0; counter < size; counter++)
  {
    //Add space
    printf(" ");
  }
}
//Print 'G' star pattern of given size
void print_g(int height)
{
  if (height < 3 || height % 2 == 0)
  {
    return;
  }
  printf("\nHeight : %d \n\n", height);
  for (int i = 0; i < height; ++i)
  {
    if (i == 0 || i == height - 1)
    {
      printf(" ");
      print_star(height - 1);
      print_space(3);
    }
    else if (i == height / 2)
    {
      //middle layer of G pattern
      printf("*");
      print_space(height / 2);
      print_star(height / 2 + 1);
      print_space(2);
    }
    else
    {
      //Intermediate layers
      printf("*");
      print_space(height - 1);
      if (i > height / 2)
      {
        printf("*");
        print_space(2);
      }
      else
      {
        print_space(3);
      }
    }
    printf("\n");
  }
}
int main()
{
  //Simple test
  print_g(7);
  print_g(9);
  print_g(5);
  print_g(11);
  return 0;
}

Output

Height : 7

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

Height : 9

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

Height : 5

 * *
*
*  * *
*    *
 * *

Height : 11

 * * * * *
*
*
*
*
*     * * *
*          *
*          *
*          *
*          *
 * * * * *
/*
  Java Program
  Print G star pattern
*/
class MyPattern
{
  //Include star of given size
  public void print_star(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      if (counter % 2 == 0)
      {
        //Add Star
        System.out.print("*");
      }
      else
      {
        //Add Space
        System.out.print(" ");
      }
    }
  }
  //Display space of given size
  public void print_space(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      //Add space
      System.out.print(" ");
    }
  }
  //Print 'G' star pattern of given size
  public void print_g(int height)
  {
    if (height < 3 || height % 2 == 0)
    {
      return;
    }
    System.out.print("\nHeight : " + height + " \n\n");
    for (int i = 0; i < height; ++i)
    {
      if (i == 0 || i == height - 1)
      {
        System.out.print(" ");
        print_star(height - 1);
        print_space(3);
      }
      else if (i == height / 2)
      {
        //middle layer of G pattern
        System.out.print("*");
        print_space(height / 2);
        print_star(height / 2 + 1);
        print_space(2);
      }
      else
      {
        //Intermediate layers
        System.out.print("*");
        print_space(height - 1);
        if (i > height / 2)
        {
          System.out.print("*");
          print_space(2);
        }
        else
        {
          print_space(3);
        }
      }
      System.out.print("\n");
    }
  }
  public static void main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Simple test
    obj.print_g(7);
    obj.print_g(9);
    obj.print_g(5);
    obj.print_g(11);
  }
}

Output

Height : 7

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

Height : 9

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

Height : 5

 * *
*
*  * *
*    *
 * *

Height : 11

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

using namespace std;
class MyPattern
{
  public:
    //Include star of given size
    void print_star(int size)
    {
      int counter = 0;
      for (counter = 0; counter < size; counter++)
      {
        if (counter % 2 == 0)
        {
          cout << "*";
        }
        else
        {
          cout << " ";
        }
      }
    }
  //Display space of given size
  void print_space(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      cout << " ";
    }
  }
  //Print 'G' star pattern of given size
  void print_g(int height)
  {
    if (height < 3 || height % 2 == 0)
    {
      return;
    }
    cout << "\nHeight : " << height << " \n\n";
    for (int i = 0; i < height; ++i)
    {
      if (i == 0 || i == height - 1)
      {
        cout << " ";
        this->print_star(height - 1);
        this->print_space(3);
      }
      else if (i == height / 2)
      {
        cout << "*";
        this->print_space(height / 2);
        this->print_star(height / 2 + 1);
        this->print_space(2);
      }
      else
      {
        cout << "*";
        this->print_space(height - 1);
        if (i > height / 2)
        {
          cout << "*";
          this->print_space(2);
        }
        else
        {
          this->print_space(3);
        }
      }
      cout << "\n";
    }
  }
};
int main()
{
  MyPattern obj =  MyPattern();
  //Simple test
  obj.print_g(7);
  obj.print_g(9);
  obj.print_g(5);
  obj.print_g(11);
  return 0;
}

Output

Height : 7

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

Height : 9

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

Height : 5

 * *
*
*  * *
*    *
 * *

Height : 11

 * * * * *
*
*
*
*
*     * * *
*          *
*          *
*          *
*          *
 * * * * *
/*
  C# Program
  Print G star pattern
*/
using System;
class MyPattern
{
  //Include star of given size
  public void print_star(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      if (counter % 2 == 0)
      {
        //Add Star
        Console.Write("*");
      }
      else
      {
        //Add Space
        Console.Write(" ");
      }
    }
  }
  //Display space of given size
  public void print_space(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      //Add space
      Console.Write(" ");
    }
  }
  //Print 'G' star pattern of given size
  public void print_g(int height)
  {
    if (height < 3 || height % 2 == 0)
    {
      return;
    }
    Console.Write("\nHeight : " + height + " \n\n");
    for (int i = 0; i < height; i++)
    {
      if (i == 0 || i == height - 1)
      {
        Console.Write(" ");
        print_star(height - 1);
        print_space(3);
      }
      else if (i == height / 2)
      {
        //middle layer of G pattern
        Console.Write("*");
        print_space(height / 2);
        print_star(height / 2 + 1);
        print_space(2);
      }
      else
      {
        //Intermediate layers
        Console.Write("*");
        print_space(height - 1);
        if (i > height / 2)
        {
          Console.Write("*");
          print_space(2);
        }
        else
        {
          print_space(3);
        }
      }
      Console.Write("\n");
    }
  }
  public static void Main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Simple test
    obj.print_g(7);
    obj.print_g(9);
    obj.print_g(5);
    obj.print_g(11);
  }
}

Output

Height : 7

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

Height : 9

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

Height : 5

 * *
*
*  * *
*    *
 * *

Height : 11

 * * * * *
*
*
*
*
*     * * *
*          *
*          *
*          *
*          *
 * * * * *
<?php
/*
  Php Program
  Print G star pattern
*/
class MyPattern
{
  //Include star of given size
  public  function print_star($size)
  {
    $counter = 0;
    for ($counter = 0; $counter < $size; $counter++)
    {
      if ($counter % 2 == 0)
      {
        //Add Star
        echo("*");
      }
      else
      {
        //Add Space
        echo(" ");
      }
    }
  }
  //Display space of given size
  public  function print_space($size)
  {
    $counter = 0;
    for ($counter = 0; $counter < $size; $counter++)
    {
      //Add space
      echo(" ");
    }
  }
  //Print 'G' star pattern of given size
  public  function print_g($height)
  {
    if ($height < 3 || $height % 2 == 0)
    {
      return;
    }
    echo("\nHeight : ". $height ." \n\n");
    for ($i = 0; $i < $height; ++$i)
    {
      if ($i == 0 || $i == $height - 1)
      {
        echo(" ");
        $this->print_star($height - 1);
        $this->print_space(3);
      }
      else if ($i == intval($height / 2))
      {
        //middle layer of G pattern
        echo("*");
        $this->print_space(intval($height / 2));
        $this->print_star(intval($height / 2) + 1);
        $this->print_space(2);
      }
      else
      {
        //Intermediate layers
        echo("*");
        $this->print_space($height - 1);
        if ($i > intval($height / 2))
        {
          echo("*");
          $this->print_space(2);
        }
        else
        {
          $this->print_space(3);
        }
      }
      echo("\n");
    }
  }
}

function main()
{
  $obj = new MyPattern();
  //Simple test
  $obj->print_g(7);
  $obj->print_g(9);
  $obj->print_g(5);
  $obj->print_g(11);
}
main();

Output

Height : 7

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

Height : 9

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

Height : 5

 * *
*
*  * *
*    *
 * *

Height : 11

 * * * * *
*
*
*
*
*     * * *
*          *
*          *
*          *
*          *
 * * * * *
/*
  Node Js Program
  Print G star pattern
*/
class MyPattern
{
  //Include star of given size
  print_star(size)
  {
    var counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      if (counter % 2 == 0)
      {
        //Add Star
        process.stdout.write("*");
      }
      else
      {
        //Add Space
        process.stdout.write(" ");
      }
    }
  }
  //Display space of given size
  print_space(size)
  {
    var counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      //Add space
      process.stdout.write(" ");
    }
  }
  //Print 'G' star pattern of given size
  print_g(height)
  {
    if (height < 3 || height % 2 == 0)
    {
      return;
    }
    process.stdout.write("\nHeight : " + height + " \n\n");
    for (var i = 0; i < height; ++i)
    {
      if (i == 0 || i == height - 1)
      {
        process.stdout.write(" ");
        this.print_star(height - 1);
        this.print_space(3);
      }
      else if (i == parseInt(height / 2))
      {
        //middle layer of G pattern
        process.stdout.write("*");
        this.print_space(parseInt(height / 2));
        this.print_star(parseInt(height / 2) + 1);
        this.print_space(2);
      }
      else
      {
        //Intermediate layers
        process.stdout.write("*");
        this.print_space(height - 1);
        if (i > parseInt(height / 2))
        {
          process.stdout.write("*");
          this.print_space(2);
        }
        else
        {
          this.print_space(3);
        }
      }
      process.stdout.write("\n");
    }
  }
}

function main(args)
{
  var obj = new MyPattern();
  //Simple test
  obj.print_g(7);
  obj.print_g(9);
  obj.print_g(5);
  obj.print_g(11);
}
main();

Output

Height : 7

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

Height : 9

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

Height : 5

 * *
*
*  * *
*    *
 * *

Height : 11

 * * * * *
*
*
*
*
*     * * *
*          *
*          *
*          *
*          *
 * * * * *
#   Python 3 Program
#   Print G star pattern

class MyPattern :
  # Include star of given size
  def print_star(self, size) :
    counter = 0
    while (counter < size) :
      if (counter % 2 == 0) :
        print("*", end = "")
      else :
        print(" ", end = "")
      
      counter += 1
    
  
  # Display space of given size
  def print_space(self, size) :
    counter = 0
    while (counter < size) :
      # Add space
      print(" ", end = "")
      counter += 1
    
  
  # Print 'G' star pattern of given size
  def print_g(self, height) :
    if (height < 3 or height % 2 == 0) :
      return
    
    print("\nHeight : ", height ," \n")
    i = 0
    while (i < height) :
      if (i == 0 or i == height - 1) :
        print(" ", end = "")
        self.print_star(height - 1)
        self.print_space(3)
      
      elif(i == int(height / 2)) :
        # middle layer of G pattern
        print("*", end = "")
        self.print_space(int(height / 2))
        self.print_star(int(height / 2) + 1)
        self.print_space(2)
      else :
        # Intermediate layers
        print("*", end = "")
        self.print_space(height - 1)
        if (i > int(height / 2)) :
          print("*", end = "")
          self.print_space(2)
        else :
          self.print_space(3)
        
      
      print("\n", end = "")
      i += 1
    
  

def main() :
  obj = MyPattern()
  # Simple test
  obj.print_g(7)
  obj.print_g(9)
  obj.print_g(5)
  obj.print_g(11)


if __name__ == "__main__": main()

Output

Height :  7

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

Height :  9

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

Height :  5

 * *
*
*  * *
*    *
 * *

Height :  11

 * * * * *
*
*
*
*
*     * * *
*          *
*          *
*          *
*          *
 * * * * *
# Ruby Program
# Print G star pattern

class MyPattern

  # Include star of given size
  def print_star(size)
  
    counter = 0
    while (counter < size)
    
      if (counter % 2 == 0)
      
        print("*")
      else
      
        print(" ")
      end
      counter += 1
    end
  end
  # Display space of given size
  def print_space(size)
  
    counter = 0
    while (counter < size)
    
      # Add space
      print(" ")
      counter += 1
    end
  end
  # Print 'G' star pattern of given size
  def print_g(height)
  
    if (height < 3 || height % 2 == 0)
    
      return
    end
    print("\nHeight : ", height ," \n\n")
    i = 0
    while (i < height)
    
      if (i == 0 || i == height - 1)
      
        print(" ")
        self.print_star(height - 1)
        self.print_space(3)
      elsif(i == height / 2)
      
        # middle layer of G pattern
        print("*")
        self.print_space(height / 2)
        self.print_star(height / 2 + 1)
        self.print_space(2)
      else
      
        # Intermediate layers
        print("*")
        self.print_space(height - 1)
        if (i > height / 2)
        
          print("*")
          self.print_space(2)
        else
        
          self.print_space(3)
        end
      end
      print("\n")
      i += 1
    end
  end
end
def main()

  obj = MyPattern.new()
  # Simple test
  obj.print_g(7)
  obj.print_g(9)
  obj.print_g(5)
  obj.print_g(11)
end
main()

Output

Height : 7 

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

Height : 9 

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

Height : 5 

 * *    
*       
*  * *  
*    *  
 * *    

Height : 11 

 * * * * *    
*             
*             
*             
*             
*     * * *   
*          *  
*          *  
*          *  
*          *  
 * * * * *    
/*
  Scala Program
  Print G star pattern
*/
class MyPattern
{
  //Include star of given size
  def print_star(size: Int): Unit = {
    var counter: Int = 0;
    while (counter < size)
    {
      if (counter % 2 == 0)
      {
        print("*");
      }
      else
      {
        print(" ");
      }
      counter += 1;
    }
  }
  //Display space of given size
  def print_space(size: Int): Unit = {
    var counter: Int = 0;
    while (counter < size)
    {
      //Add space
      print(" ");
      counter += 1;
    }
  }
  //Print 'G' star pattern of given size
  def print_g(height: Int): Unit = {
    if (height < 3 || height % 2 == 0)
    {
      return;
    }
    print("\nHeight : " + height + " \n\n");
    var i: Int = 0;
    while (i < height)
    {
      if (i == 0 || i == height - 1)
      {
        print(" ");
        print_star(height - 1);
        print_space(3);
      }
      else if (i == (height / 2).toInt)
      {
        //middle layer of G pattern
        print("*");
        print_space((height / 2).toInt);
        print_star((height / 2).toInt + 1);
        print_space(2);
      }
      else
      {
        //Intermediate layers
        print("*");
        print_space(height - 1);
        if (i > (height / 2).toInt)
        {
          print("*");
          print_space(2);
        }
        else
        {
          print_space(3);
        }
      }
      print("\n");
      i += 1;
    }
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();
    //Simple test
    obj.print_g(7);
    obj.print_g(9);
    obj.print_g(5);
    obj.print_g(11);
  }
}

Output

Height : 7

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

Height : 9

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

Height : 5

 * *
*
*  * *
*    *
 * *

Height : 11

 * * * * *
*
*
*
*
*     * * *
*          *
*          *
*          *
*          *
 * * * * *
/*
  Swift Program
  Print G star pattern
*/
class MyPattern
{
  //Include star of given size
  func print_star(_ size: Int)
  {
    var counter: Int = 0;
    while (counter < size)
    {
      if (counter % 2 == 0)
      {
        print("*", terminator: "");
      }
      else
      {
        print(" ", terminator: "");
      }
      counter += 1;
    }
  }
  //Display space of given size
  func print_space(_ size: Int)
  {
    var counter: Int = 0;
    while (counter < size)
    {
      //Add space
      print(" ", terminator: "");
      counter += 1;
    }
  }
  //Print "F" star pattern of given size
  func print_g(_ height: Int)
  {
    if (height < 3 || height % 2 == 0)
    {
      return;
    }
    print("\nHeight : ", height ," \n\n", terminator: "");
    var i: Int = 0;
    while (i < height)
    {
      if (i == 0 || i == height - 1)
      {
        print(" ", terminator: "");
        self.print_star(height - 1);
        self.print_space(3);
      }
      else if (i == height / 2)
      {
        //middle layer of G pattern
        print("*", terminator: "");
        self.print_space(height / 2);
        self.print_star(height / 2 + 1);
        self.print_space(2);
      }
      else
      {
        //Intermediate layers
        print("*", terminator: "");
        self.print_space(height - 1);
        if (i > height / 2)
        {
          print("*", terminator: "");
          self.print_space(2);
        }
        else
        {
          self.print_space(3);
        }
      }
      print("\n", terminator: "");
      i += 1;
    }
  }
}
func main()
{
  let obj: MyPattern = MyPattern();
  //Simple test
  obj.print_g(7);
  obj.print_g(9);
  obj.print_g(5);
  obj.print_g(11);
}
main();

Output

Height :  7

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

Height :  9

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

Height :  5

 * *
*
*  * *
*    *
 * *

Height :  11

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




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