Print solid and hollow rhombus patterns
In this article, we will discuss how to print solid and hollow rhombus patterns using a C program. A rhombus is a quadrilateral with all sides of equal length. The solid rhombus pattern consists of filled asterisks (*) forming a rhombus shape, while the hollow rhombus pattern has asterisks (*) forming only the boundary of the rhombus.
Problem Statement
Given the height of the rhombus, we need to print both the solid and hollow rhombus patterns.
For example, if the height is 7, the solid and hollow rhombus patterns would look like this:
******* ******* ******* ******* ******* ******* *******
******* * * * * * * * * * * *******
Algorithm
The following steps outline the algorithm to print the solid and hollow rhombus patterns:
- Define a function to print empty spaces. This function will take the number of spaces as a parameter and print that many spaces using the
printf()
function. - Define a function to print the rhombus patterns. This function will take the height of the rhombus as a parameter.
- Within the
printRhombus()
function, iterate through each row of the rhombus pattern from 0 to height-1. - For each row, call the
includeSpace()
function to print the appropriate number of empty spaces before the pattern. - Print the solid rhombus pattern by iterating through each column of the rhombus and printing asterisks (*) using the
printf()
function. - Print a separator (e.g., 5 empty spaces) between the solid and hollow rhombus patterns.
- Print the hollow rhombus pattern by iterating through each column and checking if it falls on the boundary of the rhombus. If so, print an asterisk (*); otherwise, print an empty space.
- Print a newline character at the end of each row to move to the next row.
- Call the
printRhombus()
function with different heights to test the program.
Pseudocode
function includeSpace(n): for i = 0 to n-1: print " " function printRhombus(height): print "Given height : " + height for i = 0 to height-1: includeSpace(height - i) for j = 0 to height-1: print "*" includeSpace(5) for j = 0 to height-1: if j == 0 or j == height-1 or i == 0 or i == height-1: print "*" else: print " " print newline main: printRhombus(7) printRhombus(12) printRhombus(15)
Code Solution
// C program for
// Print solid and hollow rhombus patterns
#include <stdio.h>
// This is display empty space of given length
void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
printf(" ");
}
}
void printRhombus(int height)
{
printf("\nGiven height : %d \n", height);
for (int i = 0; i < height; ++i)
{
includeSpace(height - i);
// Solid rhombus
for (int j = 0; j < height; ++j)
{
printf("*");
}
// separator
includeSpace(5);
// hollow rhombus
for (int j = 0; j < height; ++j)
{
if (j == 0 || j + 1 == height || i == 0 || i + 1 == height)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
int main(int argc, char
const *argv[])
{
// Test
printRhombus(7);
printRhombus(12);
printRhombus(15);
return 0;
}
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
/*
Java Program
Print solid and hollow rhombus patterns
*/
public class Pattern
{
// This is display empty space of given length
public void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" ");
}
}
public void printRhombus(int height)
{
System.out.print("\nGiven height : " + height + " \n");
for (int i = 0; i < height; ++i)
{
includeSpace(height - i);
// Solid rhombus
for (int j = 0; j < height; ++j)
{
System.out.print("*");
}
// separator
includeSpace(5);
// hollow rhombus
for (int j = 0; j < height; ++j)
{
if (j == 0 || j + 1 == height || i == 0 || i + 1 == height)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
Pattern task = new Pattern();
// Test
task.printRhombus(7);
task.printRhombus(12);
task.printRhombus(15);
}
}
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Print solid and hollow rhombus patterns
*/
class Pattern
{
public:
// This is display empty space of given length
void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
cout << " ";
}
}
void printRhombus(int height)
{
cout << "\nGiven height : " << height << " \n";
for (int i = 0; i < height; ++i)
{
this->includeSpace(height - i);
// Solid rhombus
for (int j = 0; j < height; ++j)
{
cout << "*";
}
// separator
this->includeSpace(5);
// hollow rhombus
for (int j = 0; j < height; ++j)
{
if (j == 0 || j + 1 == height ||
i == 0 || i + 1 == height)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << "\n";
}
}
};
int main()
{
Pattern *task = new Pattern();
// Test
task->printRhombus(7);
task->printRhombus(12);
task->printRhombus(15);
return 0;
}
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
// Include namespace system
using System;
/*
Csharp Program
Print solid and hollow rhombus patterns
*/
public class Pattern
{
// This is display empty space of given length
public void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" ");
}
}
public void printRhombus(int height)
{
Console.Write("\nGiven height : " + height + " \n");
for (int i = 0; i < height; ++i)
{
this.includeSpace(height - i);
// Solid rhombus
for (int j = 0; j < height; ++j)
{
Console.Write("*");
}
// separator
this.includeSpace(5);
// hollow rhombus
for (int j = 0; j < height; ++j)
{
if (j == 0 || j + 1 == height ||
i == 0 || i + 1 == height)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
Pattern task = new Pattern();
// Test
task.printRhombus(7);
task.printRhombus(12);
task.printRhombus(15);
}
}
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
package main
import "fmt"
/*
Go Program
Print solid and hollow rhombus patterns
*/
type Pattern struct {}
func getPattern() * Pattern {
var me *Pattern = &Pattern {}
return me
}
// This is display empty space of given length
func(this Pattern) includeSpace(n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ")
}
}
func(this Pattern) printRhombus(height int) {
fmt.Print("\nGiven height : ", height, " \n")
for i := 0 ; i < height ; i++ {
this.includeSpace(height - i)
// Solid rhombus
for j := 0 ; j < height ; j++ {
fmt.Print("*")
}
// separator
this.includeSpace(5)
// hollow rhombus
for j := 0 ; j < height ; j++ {
if j == 0 || j + 1 == height ||
i == 0 || i + 1 == height {
fmt.Print("*")
} else {
fmt.Print(" ")
}
}
fmt.Print("\n")
}
}
func main() {
var task * Pattern = getPattern()
// Test
task.printRhombus(7)
task.printRhombus(12)
task.printRhombus(15)
}
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
<?php
/*
Php Program
Print solid and hollow rhombus patterns
*/
class Pattern
{
// This is display empty space of given length
public function includeSpace($n)
{
for ($i = 0; $i < $n; ++$i)
{
echo(" ");
}
}
public function printRhombus($height)
{
echo("\nGiven height : ".$height.
" \n");
for ($i = 0; $i < $height; ++$i)
{
$this->includeSpace($height - $i);
// Solid rhombus
for ($j = 0; $j < $height; ++$j)
{
echo("*");
}
// separator
$this->includeSpace(5);
// hollow rhombus
for ($j = 0; $j < $height; ++$j)
{
if ($j == 0 || $j + 1 == $height ||
$i == 0 || $i + 1 == $height)
{
echo("*");
}
else
{
echo(" ");
}
}
echo("\n");
}
}
}
function main()
{
$task = new Pattern();
// Test
$task->printRhombus(7);
$task->printRhombus(12);
$task->printRhombus(15);
}
main();
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
/*
Node JS Program
Print solid and hollow rhombus patterns
*/
class Pattern
{
// This is display empty space of given length
includeSpace(n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" ");
}
}
printRhombus(height)
{
process.stdout.write("\nGiven height : " + height + " \n");
for (var i = 0; i < height; ++i)
{
this.includeSpace(height - i);
// Solid rhombus
for (var j = 0; j < height; ++j)
{
process.stdout.write("*");
}
// separator
this.includeSpace(5);
// hollow rhombus
for (var j = 0; j < height; ++j)
{
if (j == 0 || j + 1 == height ||
i == 0 || i + 1 == height)
{
process.stdout.write("*");
}
else
{
process.stdout.write(" ");
}
}
process.stdout.write("\n");
}
}
}
function main()
{
var task = new Pattern();
// Test
task.printRhombus(7);
task.printRhombus(12);
task.printRhombus(15);
}
main();
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
# Python 3 Program
# Print solid and hollow rhombus patterns
class Pattern :
# This is display empty space of given length
def includeSpace(self, n) :
i = 0
while (i < n) :
print(" ", end = "")
i += 1
def printRhombus(self, height) :
print("\nGiven height : ", height ," ")
i = 0
while (i < height) :
self.includeSpace(height - i)
j = 0
# Solid rhombus
while (j < height) :
print("*", end = "")
j += 1
# separator
self.includeSpace(5)
j = 0
# hollow rhombus
while (j < height) :
if (j == 0 or j + 1 == height or
i == 0 or i + 1 == height) :
print("*", end = "")
else :
print(" ", end = "")
j += 1
print(end = "\n")
i += 1
def main() :
task = Pattern()
# Test
task.printRhombus(7)
task.printRhombus(12)
task.printRhombus(15)
if __name__ == "__main__": main()
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
# Ruby Program
# Print solid and hollow rhombus patterns
class Pattern
# This is display empty space of given length
def includeSpace(n)
i = 0
while (i < n)
print(" ")
i += 1
end
end
def printRhombus(height)
print("\nGiven height : ", height ," \n")
i = 0
while (i < height)
self.includeSpace(height - i)
j = 0
# Solid rhombus
while (j < height)
print("*")
j += 1
end
# separator
self.includeSpace(5)
j = 0
# hollow rhombus
while (j < height)
if (j == 0 || j + 1 == height ||
i == 0 || i + 1 == height)
print("*")
else
print(" ")
end
j += 1
end
print("\n")
i += 1
end
end
end
def main()
task = Pattern.new()
# Test
task.printRhombus(7)
task.printRhombus(12)
task.printRhombus(15)
end
main()
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
/*
Scala Program
Print solid and hollow rhombus patterns
*/
class Pattern()
{
// This is display empty space of given length
def includeSpace(n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" ");
i += 1;
}
}
def printRhombus(height: Int): Unit = {
print("\nGiven height : " + height + " \n");
var i: Int = 0;
while (i < height)
{
includeSpace(height - i);
var j: Int = 0;
// Solid rhombus
while (j < height)
{
print("*");
j += 1;
}
// separator
includeSpace(5);
j = 0;
// hollow rhombus
while (j < height)
{
if (j == 0 || j + 1 == height ||
i == 0 || i + 1 == height)
{
print("*");
}
else
{
print(" ");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pattern = new Pattern();
// Test
task.printRhombus(7);
task.printRhombus(12);
task.printRhombus(15);
}
}
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
/*
Swift 4 Program
Print solid and hollow rhombus patterns
*/
class Pattern
{
// This is display empty space of given length
func includeSpace(_ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", terminator: "");
i += 1;
}
}
func printRhombus(_ height: Int)
{
print("\nGiven height : ", height ," ");
var i: Int = 0;
while (i < height)
{
self.includeSpace(height - i);
var j: Int = 0;
// Solid rhombus
while (j < height)
{
print("*", terminator: "");
j += 1;
}
// separator
self.includeSpace(5);
j = 0;
// hollow rhombus
while (j < height)
{
if (j == 0 || j + 1 == height || i == 0 || i + 1 == height)
{
print("*", terminator: "");
}
else
{
print(" ", terminator: "");
}
j += 1;
}
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
let task: Pattern = Pattern();
// Test
task.printRhombus(7);
task.printRhombus(12);
task.printRhombus(15);
}
main();
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
/*
Kotlin Program
Print solid and hollow rhombus patterns
*/
class Pattern
{
// This is display empty space of given length
fun includeSpace(n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" ");
i += 1;
}
}
fun printRhombus(height: Int): Unit
{
print("\nGiven height : " + height + " \n");
var i: Int = 0;
while (i < height)
{
this.includeSpace(height - i);
var j: Int = 0;
// Solid rhombus
while (j < height)
{
print("*");
j += 1;
}
// separator
this.includeSpace(5);
j = 0;
// hollow rhombus
while (j < height)
{
if (j == 0 || j + 1 == height ||
i == 0 || i + 1 == height)
{
print("*");
}
else
{
print(" ");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Pattern = Pattern();
// Test
task.printRhombus(7);
task.printRhombus(12);
task.printRhombus(15);
}
Output
Given height : 7
******* *******
******* * *
******* * *
******* * *
******* * *
******* * *
******* *******
Given height : 12
************ ************
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ * *
************ ************
Given height : 15
*************** ***************
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** * *
*************** ***************
Output Explanation
For each height value, the program will print the solid and hollow rhombus patterns. Each row of the patterns consists of asterisks (*) and spaces.
In the solid rhombus pattern, all the spaces inside the rhombus are filled with asterisks (*), forming a solid shape.
In the hollow rhombus pattern, only the boundary of the rhombus is formed by asterisks (*), while the inner spaces are left empty.
Time Complexity
The time complexity of the code is O(n^2) because there are two nested loops iterating from 0 to the height of the rhombus.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment