Rhombus printing program

Python Methods and Functions

Examples:

C++

// C++ program for printing a diamond shape
// with 2n lines
# include & lt; bits / stdC++. h & gt;

using namespace std;

 
// Prints a 2n line diamond pattern

void printDiamond ( int n) 

  int space = n - 1; 

 

// start loop (parent loop)

  // up to the number of lines

for ( int i = 0; i & lt; n; i ++) 

 

// loop for initially spaces,

// before printing

for ( int j = 0; j & lt; space; j ++) 

cout & lt; & lt; ""

 

// Print i + 1 stars

  for ( int j = 0; j & lt; = i; j ++) 

cout & lt; & lt; "*"

 

cout & lt; & lt; endl; 

space--; 

 

  // Repeat in reverse order

space = 0; 

 

// start loop (parent loop)

  // up to the number of lines

for ( int i = n; i & gt; 0; i--) 

  // loop for original space,

// before printing starts

for ( int j = 0; j & lt; space; j ++) 

  cout & lt; & lt; ""

 

// Print I am a star

  for ( int j = 0; j & lt; i; j ++) 

cout & lt; & lt; "*" ;

 

cout & lt; & lt; endl;

space ++; 

  
// Driver code

int main () 

printDiamond (5); 

return 0; 

 
// This code was added
// ratbhupendra

C

// C program for printing
// diamond with
// 2n lines
# include & lt; stdio.h & gt;

 
// Prints the diamond
// template with 2n lines

void printDiamond ( int n)

{

int space = n - 1;

 

// start loop (parent loop)

  // up to the number of lines

for ( int i = 0; i & lt; n; i ++)

  {

// loop for initially spaces,

// before printing

for ( int j = 0; j & lt; space; j ++)

printf ( "" );

 

// Print i + 1 stars

  for ( int j = 0; j & lt; = i; j ++)

printf ( " * " );

 

printf ( " " );

space--;

}

 

  // Repeat again in

// reverse order

space = 0;

 

// start loop (parent loop)

  // up to the number of lines

for ( int i = n; i & gt; 0; i--)

{

  // loop for the original space,

// before printing starts

for ( int j = 0; j & lt; space; j ++)

  printf ( "" );

 

// Print I am a star

  for ( int j = 0; j & lt; i; j ++)

printf ( "*" );

 

printf ( " " );

space ++;

}

}

  
// Driver code

int main ()

{

printDiamond (5);

return 0;

}

Java

// JAVA code for printing
// diamond shape

import java.util. *;

 

class GFG

{

  

// Prints a diamond pattern

// with 2n lines

static void printDiamond ( int n)

  {

int space = n - 1 ;

 

// start loop (parent loop)

  // up to the number of lines

for ( int i = 0 ; i & lt; n ; i ++)

{

// loop for the original space,

// before printing starts

for ( int j = 0 ; j & lt; space; j ++)

System.out.print ( " " );

 

// Print i + 1 stars

  for ( int j = 0 ; j & lt; = i; j ++)

System. out.print ( "*" );

 

System.out.print ( " " );

space--;

}

 

  // Repeat again in

// reverse order

space = 0 ;

 

// start loop (parent loop)

  // up to the number of lines

for ( int i = n; i & gt; 0 ; i--)

{

// loop for the original space,

  // before printing starts

for ( int j = 0 ; j & lt; space; j ++)

System.out.print ( " " );

 

// Print I am a star

  for ( int j = 0 ; j & lt; i; j ++)

System.out.print ( "*" );

 

System.out.print ( " " );

space ++;

}

}

 

// Driver code

public static void main (String [] args) 

{

printDiamond ( 5 );

 

}

}

  
// This code has been added
// Arnav Kr. Mandal.

python3

# Python program for
# printing diamond shape

  
# Function for printing
# Diamond shape

def Diamond (rows):

n = 0

  for i in range ( 1 , rows + 1 ):

  # loop for printing spaces

for j in range ( 1 , (rows - i) + 1 ):

print (end = " " )

 

# loop for printing an asterisk

while n! = ( 2 * i - 1 ):

print ( "*" , end = "")

n = n + 1

n = 0

 

# Line break

print () 

  

k = 1

n = 1

for i in range ( 1 , rows):

  # loop to print spaces

  for j in range ( 1 , k + 1 ):

  print (end = "" )

k = k + 1

 

# loop for printing an asterisk

while n & lt; = ( 2 * (rows - i) - 1 ):

print ( "*" , end = "")

n = n + 1

n = 1

  print ()

 
Code driver
number of lines to enter

rows = 5

Diamond (rows)

C #

// C # printable code
// diamond shape

using System;

 

class GFG 

{

  

// Prints a diamond pattern

// with 2n lines

static void printDiamond ( int n)

  {

int space = n - 1;

 

// start loop (parent loop)

  // up to the number of lines

for ( int i = 0; i & lt; n; i ++)

{

// loop for the original space ,

// before printing starts

for ( int j = 0; j & lt; space; j ++)

Conso le.Write ( "" );

 

// Print i + 1 stars

  for ( int j = 0; j & lt; = i; j ++)

Console.Write ( "*" );

 

Console.Write ( " " );

space--;

}

 

  // Repeat again in

// reverse order

/ table>

C #

// C # printable code
// diamond shape

using System;

 

class GFG 

{

  

// Prints a diamond pattern

// with 2n lines

static void printDiamond ( int n)

  {

int space = n - 1;

 

// start loop (parent loop)

  // up to the number of lines

for ( int i = 0; i & lt; n; i ++)

{

// loop for the original space ,

// before printing starts