打印图案“GFG”的程序
在本文中,给定 n(字母的长度)和 k(字母的宽度)的值,我们将学习如何使用星号和空格打印图案“GFG”。
例子:
INPUT: n=7, k=5
OUTPUT:
***** ***** *****
* * *
* * *
* ** ***** * ***
* * * * *
* * * * *
***** * *****
INPUT: n=11, k=7
OUTPUT:
******* ******* *******
* * *
* * *
* * *
* * *
* ***** ******* * *****
* * * * *
* * * * *
* * * * *
* * * * *
******* * *******
C++
#include
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
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
输出 :
***** ***** *****
* * *
* * *
* ** ***** * ***
* * * * *
* * * * *
***** * *****