Program to print the diamond shape

Given a number n, write a C program to print a diamond shape with 2n rows.

Examples:

C/C++

```// C program to print diamond shape with 2n rows
#include<stdio.h>

// Prints diamond pattern with 2n rows
void printDiamond(int n)
{
int space = n-1;

// run loop (parent loop) till number of rows
for (int i=0; i<n; i++)
{
// loop for initially space, before
// star printing
for (int j=0;j< space;j++)
printf(" ");

// Print i+1 stars
for (int j=0;j<=i;j++)
printf("* ");

printf("\n");
space--;
}

// Repeat again in reverse order
space = 0;

// run loop (parent loop) till number of rows
for (int i=n; i>0; i--)
{
// loop for initially space, before
// star printing
for (int j=0; j<space; j++)
printf(" ");

// Print i stars
for (int j=0;j< i;j++)
printf("* ");

printf("\n");
space++;
}
}

// Driver code
int main()
{
printDiamond(5);
return 0;
}
```

Java

```// JAVA Code to print the diamond shape
import java.util.*;

class GFG {

// Prints diamond pattern with 2n rows
static void printDiamond(int n)
{
int space = n - 1;

// run loop (parent loop) till number
// of rows
for (int i = 0; i < n; i++)
{
// loop for initially space, before
// star printing
for (int j = 0; j < space; j++)
System.out.print(" ");

// Print i+1 stars
for (int j = 0; j <= i; j++)
System.out.print("* ");

System.out.print("\n");
space--;
}

// Repeat again in reverse order
space = 0;

// run loop (parent loop) till number
// of rows
for (int i = n; i > 0; i--)
{
// loop for initially space, before
// star printing
for (int j = 0; j < space; j++)
System.out.print(" ");

// Print i stars
for (int j = 0; j < i; j++)
System.out.print("* ");

System.out.print("\n");
space++;
}
}

/* Driver program to test above function */
public static void main(String[] args)
{
printDiamond(5);

}
}

// This code is contributed by Arnav Kr. Mandal.
```

Python

```# Python program to print Diamond shape

# Function to print Diamond shape
def Diamond(rows):
n = 0
for i in range(1, rows+1):
# loop to print spaces
for j in range (1, (rows-i)+1):
print(end=" ")

# loop to print star
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 to print star
while n <= (2*(rows-i)-1):
print("*", end="")
n = n+1
n = 1
print()

# Driver program
# number of rows input
rows = 5
Diamond(rows)
```

Output:

```
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
```

This article is contributed by Rahul Singh(Nit KKR). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1 Average Difficulty : 1/5.0
Based on 2 vote(s)