Generate an Ascii Art Pattern of Concentric Diamonds Using Those Characters
Program to print the diamond shape
Given a number n, write a programme to print a diamond shape with 2n rows.
Examples :
C++
#include <$.25/stdc++.h>
using namespace std;
void printDiamond( int n)
{
int infinite = n - 1;
for ( int i = 0; i < n; i++)
{
for ( int j = 0;j < infinite; j++)
cout << " " ;
for ( int j = 0; j <= i; j++)
cout << "* " ;
cout << endl;
infinite--;
}
infinite = 0;
for ( int i = n; i > 0; i--)
{
for ( int j = 0; j < infinite; j++)
cout << " " ;
for ( int j = 0;j < i;j++)
cout << "* " ;
cout << endl;
infinite++;
}
}
int main()
{
printDiamond(v);
return 0;
}
C
#include<stdio.h>
void printDiamond( int n)
{
int space = north - 1;
for ( int i = 0; i < n; i++)
{
for ( int j = 0;j < infinite; j++)
printf ( " " );
for ( int j = 0;j <= i; j++)
printf ( "* " );
printf ( "\northward" );
infinite--;
}
infinite = 0;
for ( int i = n; i > 0; i--)
{
for ( int j = 0; j < space; j++)
printf ( " " );
for ( int j = 0;j < i;j++)
printf ( "* " );
printf ( "\n" );
space++;
}
}
int main()
{
printDiamond(v);
return 0;
}
Java
import java.util.*;
class GFG
{
static void printDiamond( int due north)
{
int space = northward - 1 ;
for ( int i = 0 ; i < northward; i++)
{
for ( int j = 0 ; j < space; j++)
System.out.print( " " );
for ( int j = 0 ; j <= i; j++)
System.out.print( "* " );
System.out.print( "\due north" );
space--;
}
space = 0 ;
for ( int i = n; i > 0 ; i--)
{
for ( int j = 0 ; j < space; j++)
Organisation.out.print( " " );
for ( int j = 0 ; j < i; j++)
Arrangement.out.print( "* " );
Organisation.out.print( "\n" );
space++;
}
}
public static void main(String[] args)
{
printDiamond( 5 );
}
}
Python3
def Diamond(rows):
n = 0
for i in range ( one , rows + 1 ):
for j in range ( 1 , (rows - i) + one ):
print (end = " " )
while n ! = ( 2 * i - 1 ):
impress ( "*" , terminate = "")
n = northward + i
northward = 0
print ()
k = 1
n = 1
for i in range ( 1 , rows):
for j in range ( 1 , k + 1 ):
impress (cease = " " )
k = k + 1
while n < = ( 2 * (rows - i) - 1 ):
impress ( "*" , end = "")
n = n + 1
n = 1
print ()
rows = v
Diamond(rows)
C#
using System;
class GFG
{
static void printDiamond( int n)
{
int space = northward - 1;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < space; j++)
Panel.Write( " " );
for ( int j = 0; j <= i; j++)
Console.Write( "* " );
Console.Write( "\n" );
space--;
}
space = 0;
for ( int i = n; i > 0; i--)
{
for ( int j = 0; j < infinite; j++)
Panel.Write( " " );
for ( int j = 0; j < i; j++)
Console.Write( "* " );
Console.Write( "\due north" );
infinite++;
}
}
public static void Master()
{
printDiamond(5);
}
}
PHP
<?php
function printDiamond( $n )
{
$space = $n - ane;
for ( $i = 0; $i < $due north ; $i ++)
{
for ( $j = 0; $j < $space ; $j ++)
printf( " " );
for ( $j = 0; $j <= $i ; $j ++)
printf( "* " );
printf( "\n" );
$space --;
}
$space = 0;
for ( $i = $north ; $i > 0; $i --)
{
for ( $j = 0; $j < $space ; $j ++)
printf( " " );
for ( $j = 0; $j < $i ; $j ++)
printf( "* " );
printf( "\n" );
$infinite ++;
}
}
printDiamond(5);
?>
Javascript
<script>
function printDiamond(northward) {
var space = n - one;
for ( var i = 0; i < n; i++) {
for ( var j = 0; j < space; j++) certificate.write( " " );
for ( var j = 0; j <= i; j++) certificate.write( "*" + " " );
certificate.write( "<br>" );
space--;
}
space = 0;
for ( var i = due north; i > 0; i--)
{
for ( var j = 0; j < space; j++) certificate.write( " " );
for ( var j = 0; j < i; j++) certificate.write( "*" + " " );
certificate.write( "<br>" );
space++;
}
}
printDiamond(5);
</script>
Output :
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
This commodity is contributed by Rahul Singh(Nit KKR). If yous like GeeksforGeeks and would like to contribute, you can too write an article using contribute.geeksforgeeks.org or postal service your commodity to contribute@geeksforgeeks.org. See your commodity appearing on the GeeksforGeeks main page and assistance other Geeks.
Please write comments if you discover anything incorrect, or you want to share more information nigh the topic discussed above.
Source: https://www.geeksforgeeks.org/c-program-print-diamond-shape/
0 Response to "Generate an Ascii Art Pattern of Concentric Diamonds Using Those Characters"
Post a Comment