Program to print the pattern ‘D’
Last Updated :
17 Feb, 2023
In this article, we will learn how to print the pattern D using stars and white-spaces. Given a number n, we will write a program to print the pattern D over n lines or rows.
Examples :
Input : n = 9
Output :
******
* *
* *
* *
* *
* *
* *
* *
******
Input : n = 12
Output :
*********
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*********
If we try to analyze this picture with a (row, column) matrix and the green circles represent the position of stars in the pattern D, we will learn the steps. Here we are performing the operations column-wise. So, for the first line of stars, we set the first if condition, where the column position with 1 gets the stars. Then all the columns greater than 1 and less than (n-2) and row position equal to 1 and n-1 get the stars. Finally, all the columns with value n-2 and row not equal to 0 or 6 get the stars, thus forming the pattern “D”. The other steps are self-explanatory and can be understood from the position of rows and columns in the diagram.
Note : Choose a minimum value of 4 for “n” to get a visible demonstration of “D” pattern.
C++
#include <bits/stdc++.h>
using namespace std;
void D_Pattern( int n){
for ( int i = 0; i < n; i++){
for ( int j = 0; j <= n; j++){
if (j == 1 || ((i == 0 || i == n-1) &&
(j > 1 && j < n-2)) || (j == n-2 &&
i != 0 && i != n-1))
cout<< "*" ;
else
cout<< " " ;
}
cout<< endl;
}
}
int main()
{
int n = 9;
D_Pattern(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void D_Pattern( int n){
for ( int i = 0 ; i < n; i++){
for ( int j = 0 ; j <= n; j++){
if (j == 1 || ((i == 0 ||
i == n- 1 ) &&
(j > 1 && j < n- 2 )) ||
(j == n- 2 && i != 0 &&
i != n- 1 ))
System.out.print( "*" );
else
System.out.print( " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int n = 9 ;
D_Pattern(n);
}
}
|
Python3
def D_Pattern(string, n):
for i in range ( 0 , n):
for j in range ( 0 , n):
if (j = = 1 or ((i = = 0 or i = = n - 1 ) and
(j > 1 and j < n - 2 )) or (j = = n - 2 and
i ! = 0 and i ! = n - 1 )):
string = string + "*"
else :
string = string + " "
string = string + "\n"
return (string);
string = "";
n = 9
print (D_Pattern(string, n));
|
C#
using System;
class GFG
{
static void D_Pattern( int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j <= n; j++)
{
if (j == 1 || ((i == 0 ||
i == n - 1) &&
(j > 1 && j < n - 2)) ||
(j == n - 2 && i != 0 &&
i != n - 1))
Console.Write( "*" );
else
Console.Write( " " );
}
Console.WriteLine();
}
}
static public void Main ()
{
int n = 9;
D_Pattern(n);
}
}
|
PHP
<?php
function D_Pattern( $n ){
# loop for rows
for ( $i = 0; $i < $n ; $i ++){
# loop for columns
for ( $j = 0; $j <= $n ; $j ++){
# Logic to generate stars for *
if ( $j == 1 or (( $i == 0 or $i == $n -1) and
( $j > 1 and $j < $n -2)) or ( $j == $n -2 and
$i != 0 and $i != $n -1))
echo "*" ;
# For the spaces
else
echo " " ;
}
# For changing line
echo "\n" ;
}
}
$n = 9;
D_Pattern( $n )
?>
|
Javascript
<script>
function D_Pattern(n) {
for ( var i = 0; i < n; i++) {
for ( var j = 0; j <= n; j++) {
if (
j == 1 ||
((i == 0 || i == n - 1) && j > 1 && j < n - 2) ||
(j == n - 2 && i != 0 && i != n - 1)
)
document.write( "*" );
else document.write( " " );
}
document.write( "<br>" );
}
}
var n = 9;
D_Pattern(n);
</script>
|
Output :
******
* *
* *
* *
* *
* *
* *
* *
******
Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...