Program to print the diamond shape - GeeksforGeeks

08 Apr, 2021
Given a number n, write a program to print a diamond shape with 2n rows.Examples :  
 
 
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to print diamond shape// with 2n rows#include <bits/stdc++.h>using namespace std; // Prints diamond pattern with 2n rowsvoid 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++)            cout << " ";         // Print i+1 stars        for (int j = 0; j <= i; j++)            cout << "* ";         cout << endl;        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++)            cout << " ";         // Print i stars        for (int j = 0;j < i;j++)            cout << "* ";         cout << endl;        space++;    }} // Driver codeint main(){    printDiamond(5);    return 0;} // This is code is contributed// by rathbhupendra
// C program to print// diamond shape with// 2n rows#include<stdio.h> // Prints diamond// pattern with 2n rowsvoid 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 codeint main(){    printDiamond(5);    return 0;}
// JAVA Code to print// the diamond shapeimport 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 Code    public static void main(String[] args)    {        printDiamond(5);             }} // This code is contributed// by Arnav Kr. Mandal.
# Python program to# print Diamond shape # Function to print# Diamond shapedef 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 Code# number of rows inputrows = 5Diamond(rows)
// C# Code to print// the diamond shapeusing System; 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++)                Console.Write(" ");                 // Print i+1 stars            for (int j = 0; j <= i; j++)                Console.Write("* ");                 Console.Write("\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++)                Console.Write(" ");                 // Print i stars            for (int j = 0; j < i; j++)                Console.Write("* ");                 Console.Write("\n");            space++;        }    }         // Driver Code    public static void Main()    {        printDiamond(5);             }} // This code is contributed// by Smitha Semwal.
<?php// PHP program to print// diamond shape with// 2n rows // Prints diamond $// pattern with 2n rowsfunction printDiamond($n){    $space = $n - 1;     // run loop (parent loop)    // till number of rows    for ($i = 0; $i < $n; $i++)    {                 // loop for initially space,        // before star printing        for ($j = 0;$j < $space; $j++)            printf(" ");         // Print i+1 stars        for ($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 ($i = $n; $i > 0; $i--)    {                 // loop for initially space,        // before star printing        for ($j = 0; $j < $space; $j++)            printf(" ");         // Pr$i stars        for ($j = 0;$j < $i;$j++)            printf("* ");         printf("\n");        $space++;    }}     // Driver code    printDiamond(5); // This code is contributed by Anuj_67?>
<script>      // JavaScript program to print diamond shape      // with 2n rows       // Prints diamond pattern with 2n rows      function printDiamond(n) {        var space = n - 1;         // run loop (parent loop)        // till number of rows        for (var i = 0; i < n; i++) {          // loop for initially space,          // before star printing          for (var j = 0; j < space; j++) document.write("  ");           // Print i+1 stars          for (var j = 0; j <= i; j++) document.write("*" + "  ");           document.write("<br>");          space--;        }         // Repeat again in reverse order        space = 0;         // run loop (parent loop)        // till number of rows        for (var i = n; i > 0; i--)        {                   // loop for initially space,          // before star printing          for (var j = 0; j < space; j++) document.write("  ");           // Print i stars          for (var j = 0; j < i; j++) document.write("*" + "  ");           document.write("<br>");          space++;        }      }       // Driver code      printDiamond(5);             // This code is contributed by rdtank.    </script>
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.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Smitha Dinesh Semwal
vt_m
rathbhupendra
rdtank
pattern-printing
Python Pattern-printing
C Language
C++
Python
School Programming
pattern-printing
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
Left Shift and Right Shift Operators in C/C++
fork() in C
Command line arguments in C/C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
Inheritance in C++
C++ Classes and Objects