Program to print the pattern “GFG”

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 * wherever required else cout << " "; // printing space wherever 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 * wherever required System.out.print("*"); else System.out.print(" "); // printing space wherever 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 # wherever 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 * wherever 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 * wherever required echo "*"; else // printing space wherever 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 * wherever required else document.write(" "," "); // printing space wherever 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 :
***** ***** ***** * * * * * * * ** ***** * *** * * * * * * * * * * ***** * *****
Time Complexity: O(n * k), where n and k represents the given inputs.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



