Program to print solid and hollow rhombus patterns

06 Jun, 2022
For any given number n, print Hollow and solid Squares and Rhombus made with stars(*). Examples:  
Input : n = 4
Output : 
Solid Rhombus:
   ****
  ****
 ****
****

Hollow Rhombus:
   ****
  *  *
 *  *
****
 
1. Solid Rhombus : Making Solid Rhombus is a bit similar to making solid square rather than the concept that for each ith row we have n-i blank spaces before stars as: – – – **** – – **** – **** **** 2. Hollow Rhombus : Formation of Hollow Rhombus uses the idea behind formation of hollow square and solid rhombus. A hollow square with n-i blank spaces before stars in each ith rows result in formation of hollow rhombus.  
 
C/C++
 
 
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to print// hollow and solid rhombus patterns #include <bits/stdc++.h>using namespace std;  // Function for Solid Rhombusvoid solidRhombus(int rows){    int i, j;    for (i=1; i<=rows; i++)    {        // Print trailing spaces        for (j=1; j<=rows - i; j++)            cout << " ";                     // Print stars after spaces        for (j=1; j<=rows; j++)            cout << "*";                     // Move to the next line/row        cout << "\n";    }} // Function for Rhombusvoid hollowRhombus(int rows){    int i, j;    for (i=1; i<=rows; i++)    {        // Print trailing spaces        for (j=1; j<=rows - i; j++)            cout << " ";                     // Print stars after spaces        // Print stars for each solid rows        if (i==1 || i==rows)            for (j=1; j<=rows; j++)                cout << "*";                         // stars for hollow rows        else            for (j=1; j<=rows; j++)                if (j==1 || j==rows)                    cout << "*";                else                    cout << " ";        // Move to the next line/row        cout << "\n";    }} // utility program to print all patternsvoid printPattern(int rows){    cout << "\nSolid Rhombus:\n";    solidRhombus(rows);         cout << "\nHollow Rhombus:\n";    hollowRhombus(rows);} // driver programint main(){    int rows = 5;    printPattern (rows);    return 0;}
// Java program to print// hollow and solid rhombus patternsimport java.io.*; class GFG{    // Function for Solid Rhombus    static void solidRhombus(int rows)    {        int i, j;        for (i=1; i<=rows; i++)        {            // Print trailing spaces            for (j=1; j<=rows - i; j++)                System.out.print(" ");                          // Print stars after spaces            for (j=1; j<=rows; j++)                System.out.print("*");                          // Move to the next line/row            System.out.println();        }    }      // Function for Hollow Rhombus    static void hollowRhombus(int rows)    {        int i, j;        for (i=1; i<=rows; i++)        {            // Print trailing spaces            for (j=1; j<=rows - i; j++)                System.out.print(" ");                          // Print stars after spaces            // Print stars for each solid rows            if (i==1 || i==rows)                for (j=1; j<=rows; j++)                    System.out.print("*");                              // stars for hollow rows            else                for (j=1; j<=rows; j++)                    if (j==1 || j==rows)                        System.out.print("*");                    else                        System.out.print(" ");            // Move to the next line/row            System.out.println();        }    }      // utility program to print all patterns    static void printPattern(int rows)    {        System.out.println("Solid Rhombus:");        solidRhombus(rows);              System.out.println("Hollow Rhombus:");        hollowRhombus(rows);    }         // driver program    public static void main (String[] args)    {        int rows = 5;        printPattern (rows);    }} // Contributed by Pramod Kumar
# Python 3 program to print# hollow and solid rhombus patterns # Function for Solid Rhombus def solidRhombus(rows):         for i in range (1,rows + 1):                 # Print trailing spaces                 for j in range (1,rows - i + 1):            print (end=" ")                     # Print stars after spaces                 for j in range (1,rows + 1):            print ("*",end="")                     # Move to the next line/row        print() # Function for Hollow Rhombus def hollowRhombus(rows):         for i in range (1, rows + 1):        # Print trailing spaces                 for j in range (1, rows - i + 1):            print (end=" ")                     # Print stars after spaces        # Print stars for each solid rows                 if i == 1 or i == rows:            for j in range (1, rows + 1):                print ("*",end="")                         # stars for hollow rows        else:            for j in range (1,rows+1):                if (j == 1 or j == rows):                    print ("*",end="")                else:                    print (end=" ")        # Move to the next line/row        print() # utility program to print all patternsdef printPattern(rows):         print ("Solid Rhombus:")    solidRhombus(rows)         print("\nHollow Rhombus:")    hollowRhombus(rows) # driver program if __name__ == "__main__":         rows = 5    printPattern (rows)
// C# program to print// hollow and solid rhombus patternsusing System; class GFG{    // Function for Solid Rhombus    static void solidRhombus(int rows)    {        int i, j;        for (i=1; i<=rows; i++)        {            // Print trailing spaces            for (j=1; j<=rows - i; j++)                Console.Write(" ");                         // Print stars after spaces            for (j=1; j<=rows; j++)                Console.Write("*");                         // Move to the next line/row            Console.WriteLine();        }    }     // Function for Hollow Rhombus    static void hollowRhombus(int rows)    {        int i, j;        for (i=1; i<=rows; i++)        {            // Print trailing spaces            for (j=1; j<=rows - i; j++)                Console.Write(" ");                         // Print stars after spaces            // Print stars for each solid rows            if (i==1 || i==rows)                for (j=1; j<=rows; j++)                    Console.Write("*");                             // stars for hollow rows            else                for (j=1; j<=rows; j++)                    if (j==1 || j==rows)                        Console.Write("*");                    else                    Console.Write(" ");            // Move to the next line/row            Console.WriteLine();        }    }     // utility program to print all patterns    static void printPattern(int rows)    {        Console.WriteLine("\nSolid Rhombus:\n");        solidRhombus(rows);             Console.WriteLine("\nHollow Rhombus:\n");        hollowRhombus(rows);    }         // driver program    public static void Main ()     {        int rows = 5;        printPattern (rows);    }} // Contributed by vt_m.
<?php// php program to print hollow`// and solid rhombus patterns // Function for Solid Rhombusfunction solidRhombus($rows){     for ($i = 1; $i <= $rows; $i++)    {                 // Print trailing spaces        for ($j = 1; $j <= $rows - $i; $j++)            echo " ";                     // Print stars after spaces        for ($j = 1; $j <= $rows; $j++)            echo "*";                     // Move to the next line/row        echo "\n";    }} // Function for Hollow Rhombusfunction hollowRhombus($rows){    for ($i = 1; $i <= $rows; $i++)    {                 // Print trailing spaces        for ($j = 1; $j <= $rows - $i; $j++)            echo " ";                     // Print stars after spaces        // Print stars for each solid $rows        if ($i == 1 || $i == $rows)            for ($j = 1; $j <= $rows; $j++)                echo "*";                         // stars for hollow $rows        else            for ($j = 1; $j <= $rows; $j++)                if ($j == 1 || $j == $rows)                    echo "*";                else                    echo " ";        // Move to the next line/row        echo "\n";    }} // utility program to print// all patternsfunction printPattern($rows){    echo "\nSolid Rhombus:\n";    solidRhombus($rows);         echo "\nHollow Rhombus:\n";    hollowRhombus($rows);}     // Driver Code    $rows = 5;    printPattern ($rows);     // This code is contributed by mits?>
<script>      // JavaScript program to print      // hollow and solid rhombus patterns       // Function for Solid Rhombus      function solidRhombus(rows)      {        var i, j;        for (i = 1; i <= rows; i++)        {                   // Print trailing spaces          for (j = 1; j <= rows - i; j++) document.write("  ");           // Print stars after spaces          for (j = 1; j <= rows; j++) document.write("*");           // Move to the next line/row          document.write("<br>");        }      }       // Function for Rhombus      function hollowRhombus(rows)      {        var i, j;        for (i = 1; i <= rows; i++)        {                   // Print trailing spaces          for (j = 1; j <= rows - i; j++) document.write("  ");           // Print stars after spaces          // Print stars for each solid rows          if (i == 1 || i == rows)            for (j = 1; j <= rows; j++) document.write("*");                       // stars for hollow rows          else            for (j = 1; j <= rows; j++)              if (j == 1 || j == rows) document.write("*");              else document.write("  ");                         // Move to the next line/row          document.write("<br>");        }      }       // utility program to print all patterns      function printPattern(rows)      {        document.write("Solid Rhombus:<br>");        solidRhombus(rows);         document.write("Hollow Rhombus:<br>");        hollowRhombus(rows);      }       // driver program      var rows = 5;      printPattern(rows);             // This code is contributed by rdtank.    </script>
Output:  
Solid Rhombus:
    *****
   *****
  *****
 *****
*****

Hollow Rhombus:
    *****
   *   *
  *   *
 *   *
*****
Time Complexity: O(r2), where r represents the given number of rows.Auxiliary Space: O(1), no extra space is required, so it is a constant.
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
Mithun Kumar
ukasp
saiyampaliwal
rdtank
tamanna17122007
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.