Print inverted diamond pattern
In this article, we will discuss how to print an inverted diamond pattern using C programming language. The program takes an input height value and generates a diamond pattern consisting of asterisks (*) and spaces.
Problem Statement
Given a height value, we need to print an inverted diamond pattern using asterisks. The diamond pattern should have a symmetrical shape, with the top half representing the upper part of the diamond and the bottom half representing the lower part of the diamond.
Example
Let's understand the problem with an example. Suppose we have a height of 5. The pattern for height 5 would look like this:
********** **** **** *** *** ** ** * * ** ** *** *** **** **** **********
Similarly, for a height of 8, the pattern would be:
**************** ******* ******* ****** ****** ***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** ***** ****** ****** ******* ******* ****************
Algorithm and Pseudocode
To solve this problem, we can follow the steps outlined in the pseudocode below:
function includeSpace(n):
for i from 0 to n:
print a space
function printPattern(height):
print "Given height: height"
for i from 0 to height:
for j from height to i:
print an asterisk (*)
includeSpace(i * 2)
for k from height to i:
print an asterisk (*)
print a new line
for i from 2 to height:
for j from 1 to i:
print an asterisk (*)
includeSpace((height - i) * 2)
for j from 1 to i:
print an asterisk (*)
print a new line
main():
call printPattern(5)
call printPattern(8)
The includeSpace
function is responsible for printing a certain number of spaces. It takes an input value n
and prints n
spaces in a loop.
The printPattern
function takes the height of the diamond as an input and generates the pattern using nested loops. It first prints the upper half of the diamond, then the lower half. The number of spaces and asterisks is determined based on the current iteration of the loops.
Code Solution
// C program for
// Print inverted diamond pattern
#include <stdio.h>
// This is display empty space of given length
void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
printf(" ");
}
}
void printPattern(int height)
{
printf("\nGiven height : %d \n", height);
for (int i = 0; i < height; ++i)
{
for (int j = height; j > i; --j)
{
printf("*");
}
// Include space
includeSpace(i *2);
for (int k = height; k > i; --k)
{
printf("*");
}
printf("\n");
}
for (int i = 2; i <= height; ++i)
{
for (int j = 1; j <= i; ++j)
{
printf("*");
}
// Include space
includeSpace((height - i) *2);
for (int j = 1; j <= i; ++j)
{
printf("*");
}
printf("\n");
}
}
int main(int argc, char
const *argv[])
{
// Test
printPattern(5);
printPattern(8);
return 0;
}
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
/*
Java Program
Print inverted diamond pattern
*/
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 printPattern(int height)
{
System.out.print("\nGiven height : " + height + " \n");
// Top layer
for (int i = 0; i < height; ++i)
{
for (int j = height; j > i; --j)
{
System.out.print("*");
}
// Include space
includeSpace(i * 2);
for (int k = height; k > i; --k)
{
System.out.print("*");
}
System.out.print("\n");
}
// Bottom layer
for (int i = 2; i <= height; ++i)
{
for (int j = 1; j <= i; ++j)
{
System.out.print("*");
}
// Include space
includeSpace((height - i) * 2);
for (int j = 1; j <= i; ++j)
{
System.out.print("*");
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
Pattern task = new Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
}
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Print inverted diamond pattern
*/
class Pattern
{
public:
// This is display empty space of given length
void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
cout << " ";
}
}
void printPattern(int height)
{
cout << "\nGiven height : " << height << " \n";
// Top layer
for (int i = 0; i < height; ++i)
{
for (int j = height; j > i; --j)
{
cout << "*";
}
// Include space
this->includeSpace(i *2);
for (int k = height; k > i; --k)
{
cout << "*";
}
cout << "\n";
}
// Bottom layer
for (int i = 2; i <= height; ++i)
{
for (int j = 1; j <= i; ++j)
{
cout << "*";
}
// Include space
this->includeSpace((height - i) *2);
for (int j = 1; j <= i; ++j)
{
cout << "*";
}
cout << "\n";
}
}
};
int main()
{
Pattern *task = new Pattern();
// Test
task->printPattern(5);
task->printPattern(8);
return 0;
}
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
// Include namespace system
using System;
/*
Csharp Program
Print inverted diamond pattern
*/
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 printPattern(int height)
{
Console.Write("\nGiven height : " + height + " \n");
// Top layer
for (int i = 0; i < height; ++i)
{
for (int j = height; j > i; --j)
{
Console.Write("*");
}
// Include space
this.includeSpace(i * 2);
for (int k = height; k > i; --k)
{
Console.Write("*");
}
Console.Write("\n");
}
// Bottom layer
for (int i = 2; i <= height; ++i)
{
for (int j = 1; j <= i; ++j)
{
Console.Write("*");
}
// Include space
this.includeSpace((height - i) * 2);
for (int j = 1; j <= i; ++j)
{
Console.Write("*");
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
Pattern task = new Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
}
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
package main
import "fmt"
/*
Go Program
Print inverted diamond pattern
*/
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) printPattern(height int) {
fmt.Print("\nGiven height : ", height, " \n")
// Top layer
for i := 0 ; i < height ; i++ {
for j := height ; j > i ; j-- {
fmt.Print("*")
}
// Include space
this.includeSpace(i * 2)
for k := height ; k > i ; k-- {
fmt.Print("*")
}
fmt.Print("\n")
}
// Bottom layer
for i := 2 ; i <= height ; i++ {
for j := 1 ; j <= i ; j++ {
fmt.Print("*")
}
// Include space
this.includeSpace((height - i) * 2)
for j := 1 ; j <= i ; j++ {
fmt.Print("*")
}
fmt.Print("\n")
}
}
func main() {
var task * Pattern = getPattern()
// Test
task.printPattern(5)
task.printPattern(8)
}
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
<?php
/*
Php Program
Print inverted diamond pattern
*/
class Pattern
{
// This is display empty space of given length
public function includeSpace($n)
{
for ($i = 0; $i < $n; ++$i)
{
echo(" ");
}
}
public function printPattern($height)
{
echo("\nGiven height : ".$height." \n");
// Top layer
for ($i = 0; $i < $height; ++$i)
{
for ($j = $height; $j > $i; --$j)
{
echo("*");
}
// Include space
$this->includeSpace($i * 2);
for ($k = $height; $k > $i; --$k)
{
echo("*");
}
echo("\n");
}
// Bottom layer
for ($i = 2; $i <= $height; ++$i)
{
for ($j = 1; $j <= $i; ++$j)
{
echo("*");
}
// Include space
$this->includeSpace(($height - $i) * 2);
for ($j = 1; $j <= $i; ++$j)
{
echo("*");
}
echo("\n");
}
}
}
function main()
{
$task = new Pattern();
// Test
$task->printPattern(5);
$task->printPattern(8);
}
main();
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
/*
Node JS Program
Print inverted diamond pattern
*/
class Pattern
{
// This is display empty space of given length
includeSpace(n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" ");
}
}
printPattern(height)
{
process.stdout.write("\nGiven height : " + height + " \n");
// Top layer
for (var i = 0; i < height; ++i)
{
for (var j = height; j > i; --j)
{
process.stdout.write("*");
}
// Include space
this.includeSpace(i * 2);
for (var k = height; k > i; --k)
{
process.stdout.write("*");
}
process.stdout.write("\n");
}
// Bottom layer
for (var i = 2; i <= height; ++i)
{
for (var j = 1; j <= i; ++j)
{
process.stdout.write("*");
}
// Include space
this.includeSpace((height - i) * 2);
for (var j = 1; j <= i; ++j)
{
process.stdout.write("*");
}
process.stdout.write("\n");
}
}
}
function main()
{
var task = new Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
main();
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
# Python 3 Program
# Print inverted diamond pattern
class Pattern :
# This is display empty space of given length
def includeSpace(self, n) :
i = 0
while (i < n) :
print(" ", end = "")
i += 1
def printPattern(self, height) :
print("\nGiven height : ", height ," ")
i = 0
# Top layer
while (i < height) :
j = height
while (j > i) :
print("*", end = "")
j -= 1
# Include space
self.includeSpace(i * 2)
k = height
while (k > i) :
print("*", end = "")
k -= 1
print(end = "\n")
i += 1
i = 2
# Bottom layer
while (i <= height) :
j = 1
while (j <= i) :
print("*", end = "")
j += 1
# Include space
self.includeSpace((height - i) * 2)
j = 1
while (j <= i) :
print("*", end = "")
j += 1
print(end = "\n")
i += 1
def main() :
task = Pattern()
# Test
task.printPattern(5)
task.printPattern(8)
if __name__ == "__main__": main()
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
# Ruby Program
# Print inverted diamond pattern
class Pattern
# This is display empty space of given length
def includeSpace(n)
i = 0
while (i < n)
print(" ")
i += 1
end
end
def printPattern(height)
print("\nGiven height : ", height ," \n")
i = 0
# Top layer
while (i < height)
j = height
while (j > i)
print("*")
j -= 1
end
# Include space
self.includeSpace(i * 2)
k = height
while (k > i)
print("*")
k -= 1
end
print("\n")
i += 1
end
i = 2
# Bottom layer
while (i <= height)
j = 1
while (j <= i)
print("*")
j += 1
end
# Include space
self.includeSpace((height - i) * 2)
j = 1
while (j <= i)
print("*")
j += 1
end
print("\n")
i += 1
end
end
end
def main()
task = Pattern.new()
# Test
task.printPattern(5)
task.printPattern(8)
end
main()
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
/*
Scala Program
Print inverted diamond pattern
*/
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 printPattern(height: Int): Unit = {
print("\nGiven height : " + height + " \n");
var i: Int = 0;
// Top layer
while (i < height)
{
var j: Int = height;
while (j > i)
{
print("*");
j -= 1;
}
// Include space
includeSpace(i * 2);
var k: Int = height;
while (k > i)
{
print("*");
k -= 1;
}
print("\n");
i += 1;
}
i = 2;
// Bottom layer
while (i <= height)
{
var j: Int = 1;
while (j <= i)
{
print("*");
j += 1;
}
// Include space
includeSpace((height - i) * 2);
j = 1;
while (j <= i)
{
print("*");
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pattern = new Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
}
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
/*
Swift 4 Program
Print inverted diamond pattern
*/
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 printPattern(_ height: Int)
{
print("\nGiven height : ", height ," ");
var i: Int = 0;
// Top layer
while (i < height)
{
var j: Int = height;
while (j > i)
{
print("*", terminator: "");
j -= 1;
}
// Include space
self.includeSpace(i * 2);
var k: Int = height;
while (k > i)
{
print("*", terminator: "");
k -= 1;
}
print(terminator: "\n");
i += 1;
}
i = 2;
// Bottom layer
while (i <= height)
{
var j: Int = 1;
while (j <= i)
{
print("*", terminator: "");
j += 1;
}
// Include space
self.includeSpace((height - i) * 2);
j = 1;
while (j <= i)
{
print("*", terminator: "");
j += 1;
}
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
let task: Pattern = Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
main();
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
/*
Kotlin Program
Print inverted diamond pattern
*/
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 printPattern(height: Int): Unit
{
print("\nGiven height : " + height + " \n");
var i: Int = 0;
// Top layer
while (i < height)
{
var j: Int = height;
while (j > i)
{
print("*");
j -= 1;
}
// Include space
this.includeSpace(i * 2);
var k: Int = height;
while (k > i)
{
print("*");
k -= 1;
}
print("\n");
i += 1;
}
i = 2;
// Bottom layer
while (i <= height)
{
var j: Int = 1;
while (j <= i)
{
print("*");
j += 1;
}
// Include space
this.includeSpace((height - i) * 2);
j = 1;
while (j <= i)
{
print("*");
j += 1;
}
print("\n");
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Pattern = Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
Output
Given height : 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Given height : 8
****************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****************
Time Complexity
The time complexity of this program is O(n^2)
because it uses nested loops to print the pattern. The outer loop runs height
times, and the inner loops run a maximum of height
times. Therefore, the overall time complexity is quadratic in terms of the input height.
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