Open In App

Program to print solid and hollow rhombus patterns

Last Updated : 20 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++ program to print
// hollow and solid rhombus patterns
 
#include <bits/stdc++.h>
using namespace std;
 
 
// Function for Solid Rhombus
void 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 Rhombus
void 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 patterns
void printPattern(int rows)
{
    cout << "\nSolid Rhombus:\n";
    solidRhombus(rows);
     
    cout << "\nHollow Rhombus:\n";
    hollowRhombus(rows);
}
 
// driver program
int main()
{
    int rows = 5;
    printPattern (rows);
    return 0;
}


Java




// Java program to print
// hollow and solid rhombus patterns
import 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




# 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 patterns
def printPattern(rows):
     
    print ("Solid Rhombus:")
    solidRhombus(rows)
     
    print("\nHollow Rhombus:")
    hollowRhombus(rows)
 
# driver program
 
if __name__ == "__main__":
     
    rows = 5
    printPattern (rows)


C#




// C# program to print
// hollow and solid rhombus patterns
using 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
// php program to print hollow`
// and solid rhombus patterns
 
// Function for Solid Rhombus
function 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 Rhombus
function 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 patterns
function 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
?>


Javascript




<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.

Approach:

solidRhombus function:

  • Initialize variables i and j.
  • Loop from i = 1 to n:
  • Loop from j = 1 to n – i and print spaces.
  • Loop from j = 1 to n and print asterisks.
    • Print a new line.
  • End of function.
    •  

hollowRhombus function:

  • Initialize variables i and j.
  • Loop from i = 1 to n:
  • Loop from j = 1 to n – i and print spaces.
  • Loop from j = 1 to n:
  • If i = 1 or i = n or j = 1 or j = n, print an asterisk.
  • Else, print a space.
    • Print a new line.
  • End of function.

C




#include <stdio.h>
 
// function to print solid rhombus
void solidRhombus(int n) {
    int i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= n; j++) {
            printf("*");
        }
        printf("\n");
    }
}
 
// function to print hollow rhombus
void hollowRhombus(int n) {
    int i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= n; j++) {
            if (i == 1 || i == n || j == 1 || j == n) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }
}
 
int main() {
    int n = 5;
    printf("Solid Rhombus:\n");
    solidRhombus(n);
 
    printf("Hollow Rhombus:\n");
    hollowRhombus(n);
 
    return 0;
}


C++




#include <iostream>
 
// function to print solid rhombus
void solidRhombus(int n) {
    int i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++) {
            std::cout << " ";
        }
        for (j = 1; j <= n; j++) {
            std::cout << "*";
        }
        std::cout << std::endl;
    }
}
 
// function to print hollow rhombus
void hollowRhombus(int n) {
    int i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++) {
            std::cout << " ";
        }
        for (j = 1; j <= n; j++) {
            if (i == 1 || i == n || j == 1 || j == n) {
                std::cout << "*";
            } else {
                std::cout << " ";
            }
        }
        std::cout << std::endl;
    }
}
 
int main() {
    int n = 5;
    std::cout << "Solid Rhombus:" << std::endl;
    solidRhombus(n);
 
    std::cout << "Hollow Rhombus:" << std::endl;
    hollowRhombus(n);
 
    return 0;
}


Java




public class Rhombus {
 
    // function to print solid rhombus
    public static void solidRhombus(int n)
    {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= n; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
 
    // function to print hollow rhombus
    public static void hollowRhombus(int n)
    {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= n; j++) {
                if (i == 1 || i == n || j == 1 || j == n) {
                    System.out.print("*");
                }
                else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
 
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println("Solid Rhombus:");
        solidRhombus(n);
 
        System.out.println("Hollow Rhombus:");
        hollowRhombus(n);
    }
}


Python3




# function to print solid rhombus
def solidRhombus(n):
    for i in range(1, n+1):
        for j in range(1, n-i+1):
            print(" ", end="")
        for j in range(1, n+1):
            print("*", end="")
        print()
         
# function to print hollow rhombus
def hollowRhombus(n):
    for i in range(1, n+1):
        for j in range(1, n-i+1):
            print(" ", end="")
        for j in range(1, n+1):
            if i==1 or i==n or j==1 or j==n:
                print("*", end="")
            else:
                print(" ", end="")
        print()
 
n = 5
print("Solid Rhombus:")
solidRhombus(n)
 
print("Hollow Rhombus:")
hollowRhombus(n)


C#




using System;
 
public class Rhombus {
    // function to print solid rhombus
    public static void solidRhombus(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                Console.Write(" ");
            }
            for (int j = 1; j <= n; j++) {
                Console.Write("*");
            }
            Console.WriteLine();
        }
    }
 
    // function to print hollow rhombus
    public static void hollowRhombus(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                Console.Write(" ");
            }
            for (int j = 1; j <= n; j++) {
                if (i == 1 || i == n || j == 1 || j == n) {
                    Console.Write("*");
                } else {
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
        }
    }
 
    public static void Main() {
        int n = 5;
        Console.WriteLine("Solid Rhombus:");
        solidRhombus(n);
 
        Console.WriteLine("Hollow Rhombus:");
        hollowRhombus(n);
    }
}


Javascript




// Javascript code addition
 
// function to print solid rhombus
function solidRhombus(n) {
  for (let i = 1; i <= n; i++) {
    for (let j = 1; j <= n - i; j++) {
      process.stdout.write(" ");
    }
    for (let j = 1; j <= n; j++) {
      process.stdout.write("*");
    }
    process.stdout.write("\n");
  }
}
 
// function to print hollow rhombus
function hollowRhombus(n) {
  for (let i = 1; i <= n; i++) {
    for (let j = 1; j <= n - i; j++) {
      process.stdout.write(" ");
    }
    for (let j = 1; j <= n; j++) {
      if (i == 1 || i == n || j == 1 || j == n) {
        process.stdout.write("*");
      } else {
        process.stdout.write(" ");
      }
    }
    process.stdout.write("\n");
  }
}
 
const n = 5;
process.stdout.write("Solid Rhombus:\n");
solidRhombus(n);
process.stdout.write("Hollow Rhombus:\n");
hollowRhombus(n);
 
// The code is contributed by Arushi Goel.


Output

Solid Rhombus:
    *****
   *****
  *****
 *****
*****
Hollow Rhombus:
    *****
   *   *
  *   *
 *   *
*****

The time complexity of both functions is O(n^2), because there are two nested loops that iterate over all the rows and columns of the rhombus.
The space complexity is O(1), because only a constant amount of memory is used regardless of the size of the rhombus.

 



Similar Reads

Program to print solid and hollow square patterns
For any given number n, print Hollow and solid Squares and Rhombus made with stars(*). Examples: Input : n = 4 Output : Solid Square: **** **** **** **** Hollow Square: **** * * * * **** 1. Solid Square : Solid Square is easiest among all given patterns. To print Solid square with n rows, we should use two loops iterating n times both. Where the ou
6 min read
Solid square inside a hollow square | Pattern
Given the value of n, print a hollow square of side length n and inside it a solid square of side length (n - 4) using stars(*). Examples : Input : n = 6 Output : ****** * * * ** * * ** * * * ****** Input : n = 11 Output : *********** * * * ******* * * ******* * * ******* * * ******* * * ******* * * ******* * * ******* * * * *********** C/C++ Code
5 min read
Program to print hollow rectangle or square star patterns
Hollow rectangle star pattern : The task is print below hollow pattern of given dimension. ******************** * * * * * * * * ******************** Explanation: Input number of rows and columns.For rows of rectangle run the outer loop from 1 to rows.for (i = 1; i &lt; = rows; i++)For column of rectangle run the inner loop from 1 to columns. for (j
4 min read
Program to print hollow pyramid, diamond pattern and their modifications
For Prerequisite : Loops, If Else Statement1. Hollow pyramid/triangle pattern The pattern is similar to pyramid pattern. The only difference is, we will replace all internal '#' or '*' characters by space character and we will print 2*N-1 (N = number of rows in pattern) '#' or '*' characters in last row. Examples: Input: n=6 Output: # # # # # # # #
21 min read
Program to print the hollow numerical parallelogram
Program to print the hollow numerical parallelogram.Examples : Input : 7 Output : 7777777777777 666666 666666 55555 55555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 55555 55555 666666 666666 7777777777777 C/C++ Code // C++ program to print the given pattern #include &lt;bits/stdc++.h&gt; using namespace std; // Function for displaying the
9 min read
Program to print the pattern with two hollow Triangles
Given a number n, the task is to write a program to draw the following pattern using '$'. $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ Approach: The above pattern is printed by breaking it into smaller patterns: The above triangle $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $This is
16 min read
Program to Print Mirrored Hollow Parallelogram
Given the length as rows and breadth as cols, the task is to print the mirrored hollow parallelogram.Examples: Input: rows = 5, cols = 8 Output: ******** * * * * * * ******** C/C++ Code // CPP program to print hollow mirrored // parallelogram star pattern series #include &lt;iostream&gt; using namespace std; // function for creating pattern void Pa
5 min read
Program to print hollow Triangle pattern
Given the number of lines as N, the task is to form the given hollow Triangle pattern.Examples: Input: N = 6 Output: ************ ***** ***** **** **** *** *** ** ** * * Approach: Input number of rows to print from the user as N.To iterate through rows run an outer loop from number of rows till it is greater than 1. The loop structure should look l
6 min read
Program to print a Hollow Triangle inside a Triangle
Given a number N(? 8), the task is to print a Hollow Triangle inside a Triangle pattern.Example: Input: N = 9 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Approach: Let i be the index for rows and j be the index for columns. Then: For sides of outer triangle: If the index of column(j) is equals to (N - i + 1) or (
7 min read
Python Program to print hollow half diamond hash pattern
Give an integer N and the task is to print hollow half diamond pattern. Examples: Input : 6 Output : # # # # # # # # # # # # # # # # # # # # Input : 7 Output : # # # # # # # # # # # # # # # # # # # # # # # # Approach: The idea is to break the pattern into two parts: Upper part: For the upper half start the for loop with iterator (i) from 1 to n and
4 min read
Practice Tags :