Program to print solid and hollow rhombus patterns
Last Updated :
20 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void solidRhombus( int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
for (j=1; j<=rows - i; j++)
cout << " " ;
for (j=1; j<=rows; j++)
cout << "*" ;
cout << "\n" ;
}
}
void hollowRhombus( int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
for (j=1; j<=rows - i; j++)
cout << " " ;
if (i==1 || i==rows)
for (j=1; j<=rows; j++)
cout << "*" ;
else
for (j=1; j<=rows; j++)
if (j==1 || j==rows)
cout << "*" ;
else
cout << " " ;
cout << "\n" ;
}
}
void printPattern( int rows)
{
cout << "\nSolid Rhombus:\n" ;
solidRhombus(rows);
cout << "\nHollow Rhombus:\n" ;
hollowRhombus(rows);
}
int main()
{
int rows = 5;
printPattern (rows);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void solidRhombus( int rows)
{
int i, j;
for (i= 1 ; i<=rows; i++)
{
for (j= 1 ; j<=rows - i; j++)
System.out.print( " " );
for (j= 1 ; j<=rows; j++)
System.out.print( "*" );
System.out.println();
}
}
static void hollowRhombus( int rows)
{
int i, j;
for (i= 1 ; i<=rows; i++)
{
for (j= 1 ; j<=rows - i; j++)
System.out.print( " " );
if (i== 1 || i==rows)
for (j= 1 ; j<=rows; j++)
System.out.print( "*" );
else
for (j= 1 ; j<=rows; j++)
if (j== 1 || j==rows)
System.out.print( "*" );
else
System.out.print( " " );
System.out.println();
}
}
static void printPattern( int rows)
{
System.out.println( "Solid Rhombus:" );
solidRhombus(rows);
System.out.println( "Hollow Rhombus:" );
hollowRhombus(rows);
}
public static void main (String[] args)
{
int rows = 5 ;
printPattern (rows);
}
}
|
Python 3
def solidRhombus(rows):
for i in range ( 1 ,rows + 1 ):
for j in range ( 1 ,rows - i + 1 ):
print (end = " " )
for j in range ( 1 ,rows + 1 ):
print ( "*" ,end = "")
print ()
def hollowRhombus(rows):
for i in range ( 1 , rows + 1 ):
for j in range ( 1 , rows - i + 1 ):
print (end = " " )
if i = = 1 or i = = rows:
for j in range ( 1 , rows + 1 ):
print ( "*" ,end = "")
else :
for j in range ( 1 ,rows + 1 ):
if (j = = 1 or j = = rows):
print ( "*" ,end = "")
else :
print (end = " " )
print ()
def printPattern(rows):
print ( "Solid Rhombus:" )
solidRhombus(rows)
print ( "\nHollow Rhombus:" )
hollowRhombus(rows)
if __name__ = = "__main__" :
rows = 5
printPattern (rows)
|
C#
using System;
class GFG
{
static void solidRhombus( int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
for (j=1; j<=rows - i; j++)
Console.Write( " " );
for (j=1; j<=rows; j++)
Console.Write( "*" );
Console.WriteLine();
}
}
static void hollowRhombus( int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
for (j=1; j<=rows - i; j++)
Console.Write( " " );
if (i==1 || i==rows)
for (j=1; j<=rows; j++)
Console.Write( "*" );
else
for (j=1; j<=rows; j++)
if (j==1 || j==rows)
Console.Write( "*" );
else
Console.Write( " " );
Console.WriteLine();
}
}
static void printPattern( int rows)
{
Console.WriteLine( "\nSolid Rhombus:\n" );
solidRhombus(rows);
Console.WriteLine( "\nHollow Rhombus:\n" );
hollowRhombus(rows);
}
public static void Main () {
int rows = 5;
printPattern (rows);
}
}
|
PHP
<?php
function solidRhombus( $rows )
{
for ( $i = 1; $i <= $rows ; $i ++)
{
for ( $j = 1; $j <= $rows - $i ; $j ++)
echo " " ;
for ( $j = 1; $j <= $rows ; $j ++)
echo "*" ;
echo "\n" ;
}
}
function hollowRhombus( $rows )
{
for ( $i = 1; $i <= $rows ; $i ++)
{
for ( $j = 1; $j <= $rows - $i ; $j ++)
echo " " ;
if ( $i == 1 || $i == $rows )
for ( $j = 1; $j <= $rows ; $j ++)
echo "*" ;
else
for ( $j = 1; $j <= $rows ; $j ++)
if ( $j == 1 || $j == $rows )
echo "*" ;
else
echo " " ;
echo "\n" ;
}
}
function printPattern( $rows )
{
echo "\nSolid Rhombus:\n" ;
solidRhombus( $rows );
echo "\nHollow Rhombus:\n" ;
hollowRhombus( $rows );
}
$rows = 5;
printPattern ( $rows );
?>
|
Javascript
<script>
function solidRhombus(rows)
{
var i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++) document.write( " " );
for (j = 1; j <= rows; j++) document.write( "*" );
document.write( "<br>" );
}
}
function hollowRhombus(rows)
{
var i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++) document.write( " " );
if (i == 1 || i == rows)
for (j = 1; j <= rows; j++) document.write( "*" );
else
for (j = 1; j <= rows; j++)
if (j == 1 || j == rows) document.write( "*" );
else document.write( " " );
document.write( "<br>" );
}
}
function printPattern(rows)
{
document.write( "Solid Rhombus:<br>" );
solidRhombus(rows);
document.write( "Hollow Rhombus:<br>" );
hollowRhombus(rows);
}
var rows = 5;
printPattern(rows);
</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.
- 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.
- End of function.
C
#include <stdio.h>
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" );
}
}
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>
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;
}
}
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 {
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();
}
}
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
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 ()
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 {
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();
}
}
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
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 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);
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...