Print the horizontal ladder pattern

Horizontal ladder pattern example

Here given code implementation process.

//C Program 
//Print the horizontal ladder pattern
#include <stdio.h>

#include <stdlib.h>

/*Include Space of given size*/
void space(int size)
{
	int counter = 0;
	for (counter = 0; counter < size; counter++)
	{
		//Add space
		printf(" ");
	}
}
void print_symbol(int size)
{
	int counter = 0;
	for (counter = 0; counter < size; counter++)
	{
		printf("━");
	}
}
//Display horizontal ladder pattern of given odd size
void horizontal_ladder(int size)
{
	if (size < 0 || size % 2 == 0)
	{
		return;
	}
	printf("\n  Size : %d \n\n", size);
	int j = 0;
	for (int i = 0; i < size; ++i)
	{
		space(size / 2);
		for (j = 0; j < size + 1; j++)
		{
			if (i == 0 || i + 1 == size)
			{
				//When displaying top and bottom layer
				if (i == 0 && j != 0)
				{
					//when need to add upper joint
					printf("┰");
					print_symbol(size);
				}
				else if (i != 0 && j != 0)
				{
					//when need to add bottom joint
					printf("┸");
					print_symbol(size);
				}
				else
				{
					print_symbol(size);
				}
			}
			else if (j + 1 != size)
			{
				//print internal layer
				space(size);
				printf("┃");
			}
		}
		printf("\n");
	}
}
int main()
{
	//Test Cases
	horizontal_ladder(3);
	horizontal_ladder(5);
	horizontal_ladder(7);
	return 0;
}

Output

  Size : 3

 ━━━┰━━━┰━━━┰━━━
    ┃   ┃   ┃
 ━━━┸━━━┸━━━┸━━━

  Size : 5

  ━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
  ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━

  Size : 7

   ━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
   ━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
  Java Program
  Print the horizontal ladder pattern
*/
class MyPattern
{
  public void space(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      //Add space
      System.out.print(" ");
    }
  }
  public void print_symbol(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      System.out.print("━");
    }
  }
  //Display horizontal ladder pattern of given odd size
  public void horizontal_ladder(int size)
  {
    if (size < 0 || size % 2 == 0)
    {
      return;
    }
    System.out.print("\n Size : "+size+" \n\n");
    int j = 0;
    for (int i = 0; i < size; ++i)
    {
      space(size / 2);
      for (j = 0; j < size + 1; j++)
      {
        if (i == 0 || i + 1 == size)
        {
          //When displaying top and bottom layer
          if (i == 0 && j != 0)
          {
            //when need to add upper joint
            System.out.print("┰");
            print_symbol(size);
          }
          else if (i != 0 && j != 0)
          {
            //when need to add bottom joint
            System.out.print("┸");
            print_symbol(size);
          }
          else
          {
            print_symbol(size);
          }
        }
        else if (j + 1 != size)
        {
          //print internal layer
          space(size);
          System.out.print("┃");
        }
      }
      System.out.print("\n");
    }
  }
  public static void main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Test Cases
    obj.horizontal_ladder(3);
    obj.horizontal_ladder(5);
    obj.horizontal_ladder(7);
  }
}

Output

 Size : 3

 ━━━┰━━━┰━━━┰━━━
    ┃   ┃   ┃
 ━━━┸━━━┸━━━┸━━━

 Size : 5

  ━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
  ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━

 Size : 7

   ━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
   ━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
  C++ Program
  Print the horizontal ladder pattern
*/
#include<iostream>

using namespace std;
class MyPattern
{
	public: void space(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			cout << " ";
		}
	}
	void print_symbol(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			cout << "━";
		}
	}
	//Display horizontal ladder pattern of given odd size
	void horizontal_ladder(int size)
	{
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		cout << "\n Size : " << size << " \n\n";
		int j = 0;
		for (int i = 0; i < size; ++i)
		{
			this->space(size / 2);
			for (j = 0; j < size + 1; j++)
			{
				if (i == 0 || i + 1 == size)
				{
					//When displaying top and bottom layer
					if (i == 0 && j != 0)
					{
						cout << "┰";
						this->print_symbol(size);
					}
					else if (i != 0 && j != 0)
					{
						cout << "┸";
						this->print_symbol(size);
					}
					else
					{
						this->print_symbol(size);
					}
				}
				else if (j + 1 != size)
				{
					//print internal layer
					this->space(size);
					cout << "┃";
				}
			}
			cout << "\n";
		}
	}
};
int main()
{
	MyPattern obj =  MyPattern();
	//Test Cases
	obj.horizontal_ladder(3);
	obj.horizontal_ladder(5);
	obj.horizontal_ladder(7);
	return 0;
}

Output

 Size : 3

 ━━━┰━━━┰━━━┰━━━
    ┃   ┃   ┃
 ━━━┸━━━┸━━━┸━━━

 Size : 5

  ━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
  ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━

 Size : 7

   ━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
   ━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
  C# Program
  Print the horizontal ladder pattern
*/
using System;
class MyPattern
{
	public void space(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add space
			Console.Write(" ");
		}
	}
	public void print_symbol(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			Console.Write("━");
		}
	}
	//Display horizontal ladder pattern of given odd size
	public void horizontal_ladder(int size)
	{
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		Console.Write("\n Size : " + size + " \n\n");
		int j = 0;
		for (int i = 0; i < size; i++)
		{
			space(size / 2);
			for (j = 0; j < size + 1; j++)
			{
				if (i == 0 || i + 1 == size)
				{
					//When displaying top and bottom layer
					if (i == 0 && j != 0)
					{
						//when need to add upper joint
						Console.Write("┰");
						print_symbol(size);
					}
					else if (i != 0 && j != 0)
					{
						//when need to add bottom joint
						Console.Write("┸");
						print_symbol(size);
					}
					else
					{
						print_symbol(size);
					}
				}
				else if (j + 1 != size)
				{
					//print internal layer
					space(size);
					Console.Write("┃");
				}
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.horizontal_ladder(3);
		obj.horizontal_ladder(5);
		obj.horizontal_ladder(7);
	}
}

Output

 Size : 3

 ━━━┰━━━┰━━━┰━━━
    ┃   ┃   ┃
 ━━━┸━━━┸━━━┸━━━

 Size : 5

  ━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
  ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━

 Size : 7

   ━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
   ━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
<?php
/*
  Php Program
  Print the horizontal ladder pattern
*/
class MyPattern
{
	public	function space($size)
	{
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++)
		{
			//Add space
			echo(" ");
		}
	}
	public	function print_symbol($size)
	{
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++)
		{
			echo("━");
		}
	}
	//Display horizontal ladder pattern of given odd size
	public	function horizontal_ladder($size)
	{
		if ($size < 0 || $size % 2 == 0)
		{
			return;
		}
		echo("\n Size : ". $size ." \n\n");
		$j = 0;
		for ($i = 0; $i < $size; ++$i)
		{
			$this->space(intval($size / 2));
			for ($j = 0; $j < $size + 1; $j++)
			{
				if ($i == 0 || $i + 1 == $size)
				{
					//When displaying top and bottom layer
					if ($i == 0 && $j != 0)
					{
						//when need to add upper joint
						echo("┰");
						$this->print_symbol($size);
					}
					else if ($i != 0 && $j != 0)
					{
						//when need to add bottom joint
						echo("┸");
						$this->print_symbol($size);
					}
					else
					{
						$this->print_symbol($size);
					}
				}
				else if ($j + 1 != $size)
				{
					//print internal layer
					$this->space($size);
					echo("┃");
				}
			}
			echo("\n");
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Test Cases
	$obj->horizontal_ladder(3);
	$obj->horizontal_ladder(5);
	$obj->horizontal_ladder(7);
}
main();

Output

 Size : 3

 ━━━┰━━━┰━━━┰━━━
    ┃   ┃   ┃
 ━━━┸━━━┸━━━┸━━━

 Size : 5

  ━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
  ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━

 Size : 7

   ━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
   ━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
  Node Js Program
  Print the horizontal ladder pattern
*/
class MyPattern
{
	space(size)
	{
		var counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add space
			process.stdout.write(" ");
		}
	}
	print_symbol(size)
	{
		var counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			process.stdout.write("━");
		}
	}
	//Display horizontal ladder pattern of given odd size
	horizontal_ladder(size)
	{
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		process.stdout.write("\n Size : " + size + " \n\n");
		var j = 0;
		for (var i = 0; i < size; ++i)
		{
			this.space(parseInt(size / 2));
			for (j = 0; j < size + 1; j++)
			{
				if (i == 0 || i + 1 == size)
				{
					//When displaying top and bottom layer
					if (i == 0 && j != 0)
					{
						//when need to add upper joint
						process.stdout.write("┰");
						this.print_symbol(size);
					}
					else if (i != 0 && j != 0)
					{
						//when need to add bottom joint
						process.stdout.write("┸");
						this.print_symbol(size);
					}
					else
					{
						this.print_symbol(size);
					}
				}
				else if (j + 1 != size)
				{
					//print internal layer
					this.space(size);
					process.stdout.write("┃");
				}
			}
			process.stdout.write("\n");
		}
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Test Cases
	obj.horizontal_ladder(3);
	obj.horizontal_ladder(5);
	obj.horizontal_ladder(7);
}
main();

Output

 Size : 3

 ━━━┰━━━┰━━━┰━━━
    ┃   ┃   ┃
 ━━━┸━━━┸━━━┸━━━

 Size : 5

  ━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
  ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━

 Size : 7

   ━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
   ━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
#   Python 3 Program
#   Print the horizontal ladder pattern

class MyPattern :
	def space(self, size) :
		counter = 0
		while (counter < size) :
			print(" ", end = "")
			counter += 1
		
	
	def print_symbol(self, size) :
		counter = 0
		while (counter < size) :
			print("━", end = "")
			counter += 1
		
	
	# Display horizontal ladder pattern of given odd size
	def horizontal_ladder(self, size) :
		if (size < 0 or size % 2 == 0) :
			return
		
		print("\n Size : ", size ," \n")
		j = 0
		i = 0
		while (i < size) :
			self.space(int(size / 2))
			j = 0
			while (j < size + 1) :
				if (i == 0 or i + 1 == size) :
					# When displaying top and bottom layer
					if (i == 0 and j != 0) :
						print("┰", end = "")
						self.print_symbol(size)
					
					elif(i != 0 and j != 0) :
						print("┸", end = "")
						self.print_symbol(size)
					else :
						self.print_symbol(size)
					
				
				elif(j + 1 != size) :
					# print internal layer
					self.space(size)
					print("┃", end = "")
				
				j += 1
			
			print(end = "\n")
			i += 1
		
	

def main() :
	obj = MyPattern()
	# Test Cases
	obj.horizontal_ladder(3)
	obj.horizontal_ladder(5)
	obj.horizontal_ladder(7)


if __name__ == "__main__": main()

Output

 Size :  3

 ━━━┰━━━┰━━━┰━━━
    ┃   ┃   ┃
 ━━━┸━━━┸━━━┸━━━

 Size :  5

  ━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
  ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━

 Size :  7

   ━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
   ━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
#   Ruby Program
#   Print the horizontal ladder pattern

class MyPattern

	def space(size)
	
		counter = 0
		while (counter < size)
		
			print(" ")
			counter += 1
		end
	end
	def print_symbol(size)
	
		counter = 0
		while (counter < size)
		
			print("━")
			counter += 1
		end
	end
	# Display horizontal ladder pattern of given odd size
	def horizontal_ladder(size)
	
		if (size < 0 || size % 2 == 0)
		
			return
		end
		print("\n Size : ", size ," \n\n")
		j = 0
		i = 0
		while (i < size)
		
			self.space(size / 2)
			j = 0
			while (j < size + 1)
			
				if (i == 0 || i + 1 == size)
				
					# When displaying top and bottom layer
					if (i == 0 && j != 0)
					
						print("┰")
						self.print_symbol(size)
					elsif(i != 0 && j != 0)
					
						print("┸")
						self.print_symbol(size)
					else
					
						self.print_symbol(size)
					end
				elsif(j + 1 != size)
				
					# print internal layer
					self.space(size)
					print("┃")
				end
				j += 1
			end
			print("\n")
			i += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Test Cases
	obj.horizontal_ladder(3)
	obj.horizontal_ladder(5)
	obj.horizontal_ladder(7)
end
main()

Output

 Size : 3 

 ━━━┰━━━┰━━━┰━━━
    ┃   ┃   ┃
 ━━━┸━━━┸━━━┸━━━

 Size : 5 

  ━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
  ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━

 Size : 7 

   ━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
   ━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
  Scala Program
  Print the horizontal ladder pattern
*/
class MyPattern
{
	def space(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size)
		{
			print(" ");
			counter += 1;
		}
	}
	def print_symbol(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size)
		{
			print("━");
			counter += 1;
		}
	}
	//Display horizontal ladder pattern of given odd size
	def horizontal_ladder(size: Int): Unit = {
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		print("\n Size : " + size + " \n\n");
		var j: Int = 0;
		var i: Int = 0;
		while (i < size)
		{
			space((size / 2).toInt);
			j = 0;
			while (j < size + 1)
			{
				if (i == 0 || i + 1 == size)
				{
					//When displaying top and bottom layer
					if (i == 0 && j != 0)
					{
						print("┰");
						print_symbol(size);
					}
					else if (i != 0 && j != 0)
					{
						print("┸");
						print_symbol(size);
					}
					else
					{
						print_symbol(size);
					}
				}
				else if (j + 1 != size)
				{
					//print internal layer
					space(size);
					print("┃");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test Cases
		obj.horizontal_ladder(3);
		obj.horizontal_ladder(5);
		obj.horizontal_ladder(7);
	}
}

Output

 Size : 3

 ━━━┰━━━┰━━━┰━━━
    ┃   ┃   ┃
 ━━━┸━━━┸━━━┸━━━

 Size : 5

  ━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
  ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━

 Size : 7

   ━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
   ━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━
/*
  Swift Program
  Print the horizontal ladder pattern
*/
class MyPattern
{
	func space(_ size: Int)
	{
		var counter: Int = 0;
		while (counter < size)
		{
			print(" ", terminator: "");
			counter += 1;
		}
	}
	func print_symbol(_ size: Int)
	{
		var counter: Int = 0;
		while (counter < size)
		{
			print("━", terminator: "");
			counter += 1;
		}
	}
	//Display horizontal ladder pattern of given odd size
	func horizontal_ladder(_ size: Int)
	{
		if (size < 0 || size % 2 == 0)
		{
			return;
		}
		print("\n Size : ", size ," \n\n", terminator: "");
		var j: Int = 0;
		var i: Int = 0;
		while (i < size)
		{
			self.space(size / 2);
			j = 0;
			while (j < size + 1)
			{
				if (i == 0 || i + 1 == size)
				{
					//When displaying top and bottom layer
					if (i == 0 && j != 0)
					{
						print("┰", terminator: "");
						self.print_symbol(size);
					}
					else if (i != 0 && j != 0)
					{
						print("┸", terminator: "");
						self.print_symbol(size);
					}
					else
					{
						self.print_symbol(size);
					}
				}
				else if (j + 1 != size)
				{
					//print internal layer
					self.space(size);
					print("┃", terminator: "");
				}
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test Cases
	obj.horizontal_ladder(3);
	obj.horizontal_ladder(5);
	obj.horizontal_ladder(7);
}
main();

Output

 Size :  3

 ━━━┰━━━┰━━━┰━━━
    ┃   ┃   ┃
 ━━━┸━━━┸━━━┸━━━

 Size :  5

  ━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━┰━━━━━
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
       ┃     ┃     ┃     ┃     ┃
  ━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━┸━━━━━

 Size :  7

   ━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━┰━━━━━━━
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
          ┃       ┃       ┃       ┃       ┃       ┃       ┃
   ━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━┸━━━━━━━


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







© 2021, kalkicode.com, All rights reserved