Program to print Inverse Diamond pattern
Last Updated :
13 Mar, 2023
Given an integer n, the task is to print the Inverse Diamond Pattern in 2n-1 rows.
Example:
Input: n = 3
Output:
*** ***
** **
* *
** **
*** ***
Input: n = 7
Output:
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
Approach:
- The full Inverse Diamond is of 2n-1 rows for an input of n.
- The program is divided to print this pattern in two parts:
- The first part is the top half of diamond of n rows.
- This part includes 3 parts- the left triangle of *, the middle triangle of space and the right triangle of *.
- The second part is the below half of diamond of n-1 rows.
- This part also includes 3 parts- the left triangle of *, the middle triangle of space and the right triangle of *.
- Printing each part, the required Inverse Diamond Pattern is obtained.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void printDiamond( int n)
{
cout<<endl;
int i, j = 0;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++)
cout<< "*" ;
for (j = 0; j < 2 * i + 1; j++)
cout<< " " ;
for (j = i; j < n; j++)
cout<< "*" ;
cout<<endl;
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < i + 2; j++)
cout<< "*" ;
for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
cout<< " " ;
for (j = 0; j < i + 2; j++)
cout<< "*" ;
cout<<endl;
}
cout<<endl;
}
int main()
{
int n = 3;
cout<< "Inverse Diamond Pattern for n = " <<n;
printDiamond(n);
n = 7;
cout<< "\nInverse Diamond Pattern for n = " <<n;
printDiamond(n);
}
|
Java
import java.util.*;
class GFG {
static void printDiamond( int n)
{
System.out.println();
int i, j = 0 ;
for (i = 0 ; i < n; i++) {
for (j = i; j < n; j++)
System.out.print( "*" );
for (j = 0 ; j < 2 * i + 1 ; j++)
System.out.print( " " );
for (j = i; j < n; j++)
System.out.print( "*" );
System.out.println();
}
for (i = 0 ; i < n - 1 ; i++) {
for (j = 0 ; j < i + 2 ; j++)
System.out.print( "*" );
for (j = 0 ; j < 2 * (n - 1 - i) - 1 ; j++)
System.out.print( " " );
for (j = 0 ; j < i + 2 ; j++)
System.out.print( "*" );
System.out.println();
}
System.out.println();
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println( "Inverse Diamond Pattern for n = " + n);
printDiamond(n);
n = 7 ;
System.out.println( "\nInverse Diamond Pattern for n = " + n);
printDiamond(n);
}
}
|
Python3
def printDiamond(n) :
print ("")
j = 0
for i in range ( 0 ,n):
for j in range (i,n):
print ( "*" ,end = "")
for j in range ( 0 , 2 * i + 1 ):
print ( " " ,end = "")
for j in range (i,n):
print ( "*" ,end = "")
print ("")
for i in range ( 0 ,n - 1 ):
for j in range ( 0 , i + 2 ):
print ( "*" ,end = "")
for j in range ( 0 , 2 * (n - 1 - i) - 1 ):
print ( " " ,end = "")
for j in range ( 0 , i + 2 ):
print ( "*" ,end = "")
print ("")
print ("")
if __name__ = = '__main__' :
n = 3
print ( "Inverse Diamond Pattern for n = " ,n)
printDiamond(n)
n = 7
print ( "\nInverse Diamond Pattern for n = " ,n )
printDiamond(n)
|
C#
using System;
class GFG
{
static void printDiamond( int n)
{
Console.WriteLine();
int i, j = 0;
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
Console.Write( "*" );
for (j = 0; j < 2 * i + 1; j++)
Console.Write( " " );
for (j = i; j < n; j++)
Console.Write( "*" );
Console.WriteLine();
}
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < i + 2; j++)
Console.Write( "*" );
for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
Console.Write( " " );
for (j = 0; j < i + 2; j++)
Console.Write( "*" );
Console.WriteLine();
}
Console.WriteLine();
}
public static void Main()
{
int n = 3;
Console.WriteLine( "Inverse Diamond " +
"Pattern for n = " + n);
printDiamond(n);
n = 7;
Console.WriteLine( "\nInverse Diamond " +
"Pattern for n = " + n);
printDiamond(n);
}
}
|
PHP
<?php
function printDiamond( $n )
{
echo "\n" ;
$i ; $j = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = $i ; $j < $n ; $j ++)
echo ( "*" );
for ( $j = 0;
$j < 2 * $i + 1; $j ++)
echo ( " " );
for ( $j = $i ; $j < $n ; $j ++)
echo ( "*" );
echo ( "\n" );
}
for ( $i = 0; $i < $n - 1; $i ++)
{
for ( $j = 0; $j < $i + 2; $j ++)
echo ( "*" );
for ( $j = 0;
$j < 2 * ( $n - 1 - $i ) - 1; $j ++)
echo ( " " );
for ( $j = 0; $j < $i + 2; $j ++)
echo ( "*" );
echo ( "\n" );
}
echo ( "\n" );
}
$n = 3;
echo ( "Inverse Diamond Pattern for n = " );
echo ( $n );
printDiamond( $n );
$n = 7;
echo ( "\nInverse Diamond Pattern for n = " );
echo ( $n );
printDiamond( $n );
?>
|
Javascript
<script>
function printDiamond(n) {
document.write( "<br><br>" );
var i,
j = 0;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++)
document.write( "*" );
for (j = 0; j < 2 * i + 1; j++)
document.write( " " );
for (j = i; j < n; j++)
document.write( "*" );
document.write( "<br>" );
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < i + 2; j++)
document.write( "*" );
for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
document.write( " " );
for (j = 0; j < i + 2; j++)
document.write( "*" );
document.write( "<br>" );
}
document.write( "<br>" );
}
var n = 3;
document.write( "Inverse Diamond Pattern for n = " + n);
printDiamond(n);
n = 7;
document.write( "\nInverse Diamond Pattern for n = " + n);
printDiamond(n);
</script>
|
Output:
Inverse Diamond Pattern for n = 3
*** ***
** **
* *
** **
*** ***
Inverse Diamond Pattern for n = 7
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
Time Complexity : O(n2)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...