Skip to main content

Print G pattern

In this article, we will discuss how to print a G pattern using stars and spaces in the C programming language. We will provide a problem statement, an explanation with a suitable example, the algorithm, pseudocode, and an explanation of the resultant output with the time complexity of the code.

Problem Statement

Write a program to print a G pattern using stars (*) and spaces ( ). The program should take the height of the pattern as input and print the G pattern accordingly. The height should be an odd number greater than or equal to 3.

Explanation with Example

Let's understand the problem with an example. Consider the input height as 7.

The expected output for height = 7:

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

The G pattern consists of multiple rows and columns. Each row represents a line of stars and spaces. The pattern starts with a top row, followed by a middle row, and ends with a bottom row. The top row and bottom row consist of three stars and some spaces, while the middle row consists of one star, some spaces, and a few more stars.

Algorithm

1. Start the program.
2. Define a function print_star() that takes the size as input and prints a line of stars and spaces.
3. Define a function print_space() that takes the size as input and prints a line of spaces.
4. Define a function print_g() that takes the height as input and prints the G pattern.
5. Check if the height is less than 3 or an even number. If true, return from the function.
6. Print the height value.
7. Iterate through each row from 0 to height-1.
8. For the first and last row, print a space, call print_star(height - 1) to print three stars, and call print_space(3) to print three spaces.
9. For the middle row, print a star, call print_space(height/2) to print half of the height in spaces, call print_star(height/2 + 1) to print half of the height plus one stars, and call print_space(2) to print two spaces.
10. For the remaining rows, print a star, call print_space(height - 1) to print height-1 spaces, and if the row index is greater than height/2, print an additional star and call print_space(2). Otherwise, call print_space(3).
11. Print a newline character after each row.
12. End the function.
13. In the main function, test the print_g() function with different height values.
14. End the program.

Pseudocode


function print_star(size):
    for counter from 0 to size-1:
        if counter is even:
            print "*"
        else:
            print " "

function print_space(size):
    for counter from 0 to size-1:
        print " "

function print_g(height):
    if height < 3 or height is even:
        return
    print "Height : " + height
    for i from 0 to height-1:
        if i is 0 or i is height-1:
            print " "
            print_star(height - 1)
            print_space(3)
        else if i is height/2:
            print "*"
            print_space(height/2)
            print_star(height/2 + 1)
            print_space(2)
        else:
            print "*"
            print_space(height - 1)
            if i > height/2:
                print "*"
                print_space(2)
            else:
                print_space(3)
        print newline

function main():
    print_g(7)
    print_g(9)
    print_g(5)
    print_g(11)

Code Solution

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

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

Resultant Output Explanation

The given code produces the following output:

Height : 7

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

Height : 9

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

Height : 5

  * *
*
*  * *
*    *
  * *

Height : 11

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

The output matches the expected pattern for each height value. The program successfully prints the G pattern using stars and spaces as per the given input height.

Time Complexity

The time complexity of the code is O(height) because the code contains a loop that iterates from 0 to height-1, where height is the input value. The complexity increases linearly with the height of the pattern.





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