Skip to content
Related Articles

Related Articles

Improve Article

Program to print the pattern “GFG”

  • Last Updated : 14 Jun, 2021

In this article, given the value of n(length of the alphabet) and k(width of the alphabet) we will learn how to print the pattern “GFG” using stars and white-spaces. 
Examples: 
 

INPUT: n=7, k=5
OUTPUT:
***** ***** *****
*     *     *
*     *     *
* **  ***** * ***
*  *  *     *   *
*  *  *     *   *
***** *     *****

INPUT: n=11, k=7
OUTPUT:
*******  ******* *******
*        *       *
*        *       *
*        *       *
*        *       *
* *****  ******* * *****
*     *  *       *     *
*     *  *       *     *
*     *  *       *     *
*     *  *       *     *
*******  *       *******

 

C++




#include <iostream>
 
using namespace std;
 
// Function to print the pattern "GFG"
void print1(int n, int k) {
  int i, j;
  for (i = 0; i < n; i++) {
    cout << "\n";
    for (j = 0; j < (3 * k + 2); j++) {
      if ((i == 0 && j != k &&
 
           /*For printing the upper portion
             of the pattern "GFG"*/
           j != 2 * k + 1) ||
          ((i == n / 2) && (j > 1) && (j != k) &&
                             (j != 2 * k + 1) &&
 
           /* for printing the middle portion
              of the  pattern "GFG" */
           (j != 2 * k + 3)) ||
          ((i == n - 1) && (j != k) &&
 
           /* for printing the lower portion of
              the pattern   "GFG" */
           ((j <= k) || (j > 2 * k + 1)))
          || (j == 0) || (j == k + 1) || (j == (2 * k + 2)) ||
          ((j == k - 1 || j == 3 * k + 1) && (i > n / 2)))
 
        cout << "*"; // printing * where ever required
 
      else
        cout << " "; // printing space where ever required
    }
  }
}
 
// Driver code
int main() {
  int n = 7; // the length of the pattern "GFG"
  int k = 5; // the width of the pattern "GFG"
  print1(n, k);
}

Java




import java.util.Scanner;
 
public class PatternGFG // create a Class named PatternGFG
{
 
  // Function to print the pattern "GFG"
  private static void print(int n, int k) {
    for (int i = 0; i < n; i++) {
      System.out.println();
      for (int j = 0; j < (3 * k + 2); j++) {
 
         // For printing the upper portion of
         // the pattern "GFG"
        if ((i == 0 && j != k && j != 2 * k + 1) ||
            ((i == n / 2) && (j > 1) && (j != k) &&
 
             // for printing the middle portion of
             // the pattern "GFG"
             (j != 2 * k + 1) && (j != 2 * k + 3)) ||
            ((i == n - 1) && (j != k) &&
 
             // for printing the lower portion of
             // the pattern   "GFG"
             ((j <= k) || (j > 2 * k + 1)))
  
           || (j == 0) || (j == k + 1) || (j == (2 * k + 2)) ||
            ((j == k - 1 || j == 3 * k + 1) && (i > n / 2)))
 
          // printing * where ever required
          System.out.print("*");
 
        else
          System.out.print(" "); // printing space where ever required
      }
    }
  }
 
  // Driver code
  public static void main(String[] args) {
    int n = 7, k = 5; // length and width of the pattern
    print(n, k);
  }
}

Python3




# Python Program to print
# the pattern “GFG”
import math
 
# Function to print the
# pattern "GFG"
def print1(n, k) :
 
    for i in range(0, n) :
        print ("\n")
        for j in range(0, (3 * k + 2)) :
            if ((i == 0 and j != k and
 
                # For printing the
                # upper portion of
                # the pattern "GFG"
                j != 2 * k + 1) or
                ((i == math.floor(n / 2)) and
                (j > 1) and (j != k) and
                (j != 2 * k + 1) and
 
                # for printing the
                # middle portion of
                # the pattern "GFG"
                (j != 2 * k + 3)) or
                ((i == n - 1) and (j != k) and
 
                # for printing the
                # lower portion of
                # the pattern "GFG"
                ((j <= k) or (j > 2 *
                              k + 1)))     or
                (j == 0) or (j == k + 1) or
                (j == (2 * k + 2)) or
                ((j ==k - 1 or j == 3 *
                               k + 1) and
                (i > math.floor(n / 2)))) :
 
                # printing * where
                # ever required
                print ("*", end = "")
 
            else :
 
                # printing space
                # where ever required
                print (" ", end = "")
     
# Driver code
 
# the length of the
# pattern "GFG"
n = 7
 
# the width of the
# pattern "GFG"
k = 5
 
print1(n, k)
 
# This code is contributed
# by Manish Shaw(manishshaw1)

C#




// C# code for printing pattern.
using System;
 
public class GFG {
 
    // Function to print the pattern "GFG"
    private static void print(int n, int k) {
         
        for (int i = 0; i < n; i++) {
             
            Console.WriteLine();
             
            for (int j = 0; j < (3 * k + 2); j++) {
         
                // For printing the upper portion of
                // the pattern "GFG"
                if ((i == 0 && j != k && j != 2 * k + 1) ||
                    ((i == n / 2) && (j > 1) && (j != k) &&
         
                    // for printing the middle portion of
                    // the pattern "GFG"
                    (j != 2 * k + 1) && (j != 2 * k + 3)) ||
                    ((i == n - 1) && (j != k) &&
         
                    // for printing the lower portion of
                    // the pattern "GFG"
                    ((j <= k) || (j > 2 * k + 1)))
         
                             || (j == 0) || (j == k + 1) ||
                   (j == (2 * k + 2)) ||    ((j == k - 1 ||
                          j == 3 * k + 1) && (i > n / 2)))
         
                    // printing * where ever required
                    Console.Write("*");
         
                else
                    Console.Write(" ");
            }
        }
    }
     
    // Driver code
    public static void Main() {
         
        // length and width of the pattern
        int n = 7, k = 5;
         
        print(n, k);
    }
}
 
// This code is contributed by vt_m.

PHP




<?php
// PHP Program to print
// the pattern “GFG”
 
// Function to print the
// pattern "GFG"
function print1($n, $k) {
 
for ($i = 0; $i < $n; $i++) {
    echo "\n";
    for ($j = 0; $j < (3 * $k + 2); $j++) {
    if (($i == 0 && $j != $k &&
     
         // For printing the upper portion
         // of the pattern "GFG"
           $j != 2 * $k + 1) ||
        (($i == floor($n / 2)) &&
        ($j > 1) && ($j != $k) &&
        ($j != 2 * $k + 1) &&
 
        /* for printing the middle portion
            of the pattern "GFG" */
        ($j != 2 * $k + 3)) ||
        (($i == $n - 1) && ($j != $k) &&
 
        /* for printing the lower portion of
            the pattern "GFG" */
        (($j <= $k) || ($j > 2 * $k + 1)))     ||
        ($j == 0) || ($j == $k + 1) ||
        ($j == (2 * $k + 2)) ||
        (($j ==$k - 1 || $j == 3 * $k + 1) &&
        ($i > floor($n / 2))))
 
        // printing * where ever required
        echo "*";
 
    else
     
        // printing space where ever required
        echo " ";
     
    }
}
}
 
// Driver code
// the length of the pattern "GFG"
$n = 7;
 
// the width of the pattern "GFG"
$k = 5;
 
print1($n, $k);
 
// This code is contributed by Sam007
?>

Javascript




<script>
// Javascript implementation for the above approach
 
// Function to print the pattern "GFG"
function print1(n, k) {
  var i, j;
  for (i = 0; i < n; i++) {
    document.write("<br>");
    for (j = 0; j < (3 * k + 2); j++) {
      if ((i == 0 && j != k &&
 
           /*For printing the upper portion
             of the pattern "GFG"*/
           j != 2 * k + 1) ||
          ((i == n / 2) && (j > 1) && (j != k) &&
                             (j != 2 * k + 1) &&
 
           /* for printing the middle portion
              of the  pattern "GFG" */
           (j != 2 * k + 3)) ||
          ((i == n - 1) && (j != k) &&
 
           /* for printing the lower portion of
              the pattern   "GFG" */
           ((j <= k) || (j > 2 * k + 1)))
          || (j == 0) || (j == k + 1) || (j == (2 * k + 2)) ||
          ((j == k - 1 || j == 3 * k + 1) && (i > n / 2)))
 
        document.write("*"); // printing * where ever required
 
      else
        document.write(" "," "); // printing space where ever required
    }
  }
}
 
// Driver code
var n = 7; // the length of the pattern "GFG"
var k = 5; // the width of the pattern "GFG"
print1(n, k);
 
// This code is contributed by Shubham Singh
</script>

Output : 
 

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

 

Attention reader! Don’t stop learning now. Participate in the Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :