Posted on by Kalkicode
Code Pattern

# Print the double sided staircase pattern

In this article, we will discuss the problem of printing a double-sided staircase pattern using a C program. We will explain the problem statement, provide a suitable example, describe the algorithm and pseudocode, and analyze the time complexity of the code. Let's dive in!

## Introduction

The double-sided staircase pattern is a common programming problem that involves printing a pattern resembling a staircase. It consists of alternating steps of spaces and symbols, forming a visually appealing structure. The goal is to write a program that can generate this pattern for any given height.

## Problem Statement

We are given a positive integer representing the height of the double-sided staircase. The task is to print the pattern using spaces and symbols. Each step of the staircase is composed of two symbols: a solid block and an intermediate symbol. The pattern should be aligned to the right.

### Example:

Let's take an example to illustrate the problem. Consider a height of 5. The expected pattern output would be as follows:

```                ░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
```

In the above example, the staircase pattern is displayed with the solid block symbol (░) and the intermediate symbol (░░).

## Algorithm and Pseudocode

To solve this problem, we can follow a simple algorithm:

1. Define a function to include a given character a certain number of times.
2. Create a function to print the double-sided staircase pattern, taking the height as input.
3. Iterate through each row of the pattern using an outer loop.
4. Include the appropriate number of spaces before each row using the includeCharacter function.
5. Within each row, iterate using an inner loop to print the intermediate symbol multiple times.
6. After printing the intermediate symbols, include the pattern symbol for that row using the includeCharacter function.
7. Print a new line and repeat the process for the remaining rows.

Here is the pseudocode for the above algorithm:

```includeCharacter(icon, n)
for i = 0 to n - 1
print icon

printPattern(height)
print "Height: height"
for i = 0 to height - 1
includeCharacter(" ", (height * 2) - i * 2)
for k = 0 to i
print "░░"
includeCharacter("░", i * 2)
print new line

main()
printPattern(5)
printPattern(8)
```

## Code Solution

``````//  C program for
//  Print the double sided staircase pattern
#include <stdio.h>
// This is display given text of given length
void includeCharacter(char *icon, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%s", icon);
}
}
void printPattern(int height)
{
printf("\n Height : %d\n\n", height);
// Outer loop control the row operation
for (int i = 0; i < height; ++i)
{
// Include space
includeCharacter(" ", (height *2) - i *2);
// Inner loop control the column operation
for (int k = 0; k <= i; ++k)
{
printf("░░");
}
// Include pattern value
includeCharacter("░", i *2);
printf("\n");
}
}
int main(int argc, char
const *argv[])
{
// Test
printPattern(5);
printPattern(8);
return 0;
}``````

#### Output

`````` Height : 5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height : 8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````
``````/*
Java Program
Print the double sided staircase pattern
*/
public class Pattern
{
// This is display given text of given length
public void includeCharacter(String icon, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(icon);
}
}
public void printPattern(int height)
{
System.out.print("\n Height : " + height + "\n\n");
// Outer loop control the row operation
for (int i = 0; i < height; ++i)
{
// Include space
includeCharacter(" ", (height * 2) - i * 2);
// Inner loop control the column operation
for (int k = 0; k <= i; ++k)
{
System.out.print("░░");
}
// Include pattern value
includeCharacter("░", i * 2);
System.out.print("\n");
}
}
public static void main(String[] args)
{
// Test
}
}``````

#### Output

`````` Height : 5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height : 8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Print the double sided staircase pattern
*/
class Pattern
{
public:
// This is display given text of given length
void includeCharacter(string icon, int n)
{
for (int i = 0; i < n; ++i)
{
cout << icon;
}
}
void printPattern(int height)
{
cout << "\n Height : " << height << "\n\n";
// Outer loop control the row operation
for (int i = 0; i < height; ++i)
{
// Include space
this->includeCharacter(" ", (height *2) - i *2);
// Inner loop control the column operation
for (int k = 0; k <= i; ++k)
{
cout << "░░";
}
// Include pattern value
this->includeCharacter("░", i *2);
cout << "\n";
}
}
};
int main()
{
// Test
return 0;
}``````

#### Output

`````` Height : 5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height : 8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````
``````// Include namespace system
using System;
/*
Csharp Program
Print the double sided staircase pattern
*/
public class Pattern
{
// This is display given text of given length
public void includeCharacter(String icon, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(icon);
}
}
public void printPattern(int height)
{
Console.Write("\n Height : " + height + "\n\n");
// Outer loop control the row operation
for (int i = 0; i < height; ++i)
{
// Include space
this.includeCharacter(" ", (height * 2) - i * 2);
// Inner loop control the column operation
for (int k = 0; k <= i; ++k)
{
Console.Write("░░");
}
// Include pattern value
this.includeCharacter("░", i * 2);
Console.Write("\n");
}
}
public static void Main(String[] args)
{
// Test
}
}``````

#### Output

`````` Height : 5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height : 8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````
``````package main
import "fmt"
/*
Go Program
Print the double sided staircase pattern
*/
type Pattern struct {}
func getPattern() * Pattern {
var me *Pattern = &Pattern {}
return me
}
// This is display given text of given length
func(this Pattern) includeCharacter(icon string, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(icon)
}
}
func(this Pattern) printPattern(height int) {
fmt.Print("\n Height : ", height, "\n\n")
// Outer loop control the row operation
for i := 0 ; i < height ; i++ {
// Include space
this.includeCharacter(" ", (height * 2) - i * 2)
// Inner loop control the column operation
for k := 0 ; k <= i ; k++ {
fmt.Print("░░")
}
// Include pattern value
this.includeCharacter("░", i * 2)
fmt.Print("\n")
}
}
func main() {
var task * Pattern = getPattern()
// Test
}``````

#### Output

`````` Height : 5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height : 8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````
``````<?php
/*
Php Program
Print the double sided staircase pattern
*/
class Pattern
{
// This is display given text of given length
public	function includeCharacter(\$icon, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(\$icon);
}
}
public	function printPattern(\$height)
{
echo("\n Height : ".\$height."\n\n");
// Outer loop control the row operation
for (\$i = 0; \$i < \$height; ++\$i)
{
// Include space
\$this->includeCharacter(" ", (\$height * 2) - \$i * 2);
// Inner loop control the column operation
for (\$k = 0; \$k <= \$i; ++\$k)
{
echo("░░");
}
// Include pattern value
\$this->includeCharacter("░", \$i * 2);
echo("\n");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

`````` Height : 5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height : 8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````
``````/*
Node JS Program
Print the double sided staircase pattern
*/
class Pattern
{
// This is display given text of given length
includeCharacter(icon, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(icon);
}
}
printPattern(height)
{
process.stdout.write("\n Height : " + height + "\n\n");
// Outer loop control the row operation
for (var i = 0; i < height; ++i)
{
// Include space
this.includeCharacter(" ", (height * 2) - i * 2);
// Inner loop control the column operation
for (var k = 0; k <= i; ++k)
{
process.stdout.write("░░");
}
// Include pattern value
this.includeCharacter("░", i * 2);
process.stdout.write("\n");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

`````` Height : 5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height : 8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````
``````#    Python 3 Program
#    Print the double sided staircase pattern
class Pattern :
#  This is display given text of given length
def includeCharacter(self, icon, n) :
i = 0
while (i < n) :
print(icon, end = "")
i += 1

def printPattern(self, height) :
print("\n Height : ", height ,"\n")
i = 0
#  Outer loop control the row operation
while (i < height) :
#  Include space
self.includeCharacter(" ", (height * 2) - i * 2)
k = 0
#  Inner loop control the column operation
while (k <= i) :
print("░░", end = "")
k += 1

#  Include pattern value
self.includeCharacter("░", i * 2)
print(end = "\n")
i += 1

def main() :
#  Test

if __name__ == "__main__": main()``````

#### Output

`````` Height :  5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height :  8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````
``````#    Ruby Program
#    Print the double sided staircase pattern
class Pattern
#  This is display given text of given length
def includeCharacter(icon, n)
i = 0
while (i < n)
print(icon)
i += 1
end

end

def printPattern(height)
print("\n Height : ", height ,"\n\n")
i = 0
#  Outer loop control the row operation
while (i < height)
#  Include space
self.includeCharacter(" ", (height * 2) - i * 2)
k = 0
#  Inner loop control the column operation
while (k <= i)
print("░░")
k += 1
end

#  Include pattern value
self.includeCharacter("░", i * 2)
print("\n")
i += 1
end

end

end

def main()
#  Test
end

main()``````

#### Output

`````` Height : 5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height : 8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
``````
``````/*
Scala Program
Print the double sided staircase pattern
*/
class Pattern()
{
// This is display given text of given length
def includeCharacter(icon: String, n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(icon);
i += 1;
}
}
def printPattern(height: Int): Unit = {
print("\n Height : " + height + "\n\n");
var i: Int = 0;
// Outer loop control the row operation
while (i < height)
{
// Include space
includeCharacter(" ", (height * 2) - i * 2);
var k: Int = 0;
// Inner loop control the column operation
while (k <= i)
{
print("░░");
k += 1;
}
// Include pattern value
includeCharacter("░", i * 2);
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pattern = new Pattern();
// Test
}
}``````

#### Output

`````` Height : 5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height : 8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````
``````/*
Swift 4 Program
Print the double sided staircase pattern
*/
class Pattern
{
// This is display given text of given length
func includeCharacter(_ icon: String, _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(icon, terminator: "");
i += 1;
}
}
func printPattern(_ height: Int)
{
print("\n Height : ", height ,"\n");
var i: Int = 0;
// Outer loop control the row operation
while (i < height)
{
// Include space
self.includeCharacter(" ", (height * 2) - i * 2);
var k: Int = 0;
// Inner loop control the column operation
while (k <= i)
{
print("░░", terminator: "");
k += 1;
}
// Include pattern value
self.includeCharacter("░", i * 2);
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
// Test
}
main();``````

#### Output

`````` Height :  5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height :  8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````
``````/*
Kotlin Program
Print the double sided staircase pattern
*/
class Pattern
{
// This is display given text of given length
fun includeCharacter(icon: String, n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(icon);
i += 1;
}
}
fun printPattern(height: Int): Unit
{
print("\n Height : " + height + "\n\n");
var i: Int = 0;
// Outer loop control the row operation
while (i < height)
{
// Include space
this.includeCharacter(" ", (height * 2) - i * 2);
var k: Int = 0;
// Inner loop control the column operation
while (k <= i)
{
print("░░");
k += 1;
}
// Include pattern value
this.includeCharacter("░", i * 2);
print("\n");
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

#### Output

`````` Height : 5

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░

Height : 8

░░
░░░░░░
░░░░░░░░░░
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░``````

## Time Complexity Analysis

The time complexity of the code depends on the height of the staircase pattern. Let's denote the height as 'n'.

The outer loop runs 'n' times, and for each iteration, the inner loop runs 'i' times. Since the maximum value of 'i' is 'n-1', the time complexity of the inner loop is O(n).

Therefore, the overall time complexity of the code is O(n^2), where 'n' represents the height of the double-sided staircase pattern.

## Finally

In this article, we discussed the problem of printing a double-sided staircase pattern using a C program. We provided a detailed explanation of the problem statement, a suitable example, the algorithm, and pseudocode. Additionally, we analyzed the time complexity of the code.

By following the algorithm and pseudocode provided, you can implement the solution to generate the double-sided staircase pattern for any given height. This pattern can be useful for various applications, including visual representations and decorative purposes.

## Comment

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.

Categories
Relative Post