Skip to main content

Print H pattern

In this article, we will discuss how to print an H pattern using the C programming language. The H pattern consists of vertical and horizontal lines, forming the shape of the letter 'H' on the screen. We will write a program that can generate this pattern for a given odd size.

Problem Statement

Given an odd number as input, we need to generate an H pattern of that size. For example, if the input is 5, the output should be:

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

The H pattern consists of vertical lines on the left and right sides and a horizontal line in the middle. The vertical lines are of size (input size - 2), and the horizontal line is equal to the input size. The middle horizontal line is aligned with the center row of the pattern.

Algorithm

To solve this problem, we can follow the steps below:

  1. Check if the given size is valid. If the size is less than zero or even, we cannot form a valid H pattern. In such cases, return.
  2. For each row from 0 to size-1, do the following:
    1. Print two spaces to create the gap at the beginning of each row.
    2. Print the vertical line symbol '┃'.
    3. If the current row is the middle row (size/2), print the horizontal line symbol '━' repeatedly for size times.
    4. Otherwise, print spaces for size times to create the gap between the vertical lines.
    5. Print the vertical line symbol '┃' again.
    6. Move to the next line.

Pseudocode


function space(size):
  for counter from 0 to size-1:
    print a space

function print_symbol(size):
  for counter from 0 to size-1:
    print the horizontal line symbol '━'

function h_pattern(size):
  if size < 0 or size is even:
    return
  print "Size : size"
  for i from 0 to size-1:
    space(2)
    print the vertical line symbol '┃'
    if i is equal to (size/2):
      print_symbol(size)
    else:
      space(size)
    print the vertical line symbol '┃'
    move to the next line

main():
  h_pattern(3)
  h_pattern(5)
  h_pattern(7)

Explanation

The code starts by defining two helper functions: space(size) and print_symbol(size). The space() function prints a space ' ' size number of times, and the print_symbol() function prints the horizontal line symbol '━' size number of times.

The main function h_pattern(size) takes the input size as a parameter and checks if it is a valid size. If the size is less than zero or even, the function returns without generating the pattern.

Inside the h_pattern() function, a loop is used to iterate through each row of the pattern. For each row, two spaces are printed to create the gap at the beginning. Then, the vertical line symbol '┃' is printed.

If the current row is the middle row (size/2), the print_symbol() function is called to print the horizontal line symbol '━' repeatedly for size times. Otherwise, spaces are printed for size times to create the gap between the vertical lines.

Finally, the vertical line symbol '┃' is printed again, and the program moves to the next line. This process is repeated for each row, generating the H pattern on the screen.

Code Solution

//C Program 
//Display H patterns
#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 H pattern of given odd size
void h_pattern(int size) {
  if(size<0 || size%2==0)
  {
    return;
  }
  
  printf("\n  Size : %d \n\n", size);
  
  int i=0;

  for(i=0;i<size;i++){
    space(2);
    printf("┃");
    
    if(i==(size/2)  )
    {
      print_symbol(size);
    }
    else
    {
      space(size);
    }
    printf("┃");
    printf("\n");
  } 
 
}


int main() {
  //Test Cases
  h_pattern(3);
  h_pattern(5);
  h_pattern(7);
  return 0;
}

Output

  Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃

  Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

  Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  C++ Program
  Display H patterns
*/
#include<iostream>

using namespace std;
class MyPattern {
	public:

    /*Include Space of given size*/
    void space(int size) {
      int counter = 0;
      for (counter = 0; counter < size; counter++) {
        //Add space

        cout << " ";
      }
    }
	void print_symbol(int size) {
		int counter = 0;
		for (counter = 0; counter < size; counter++) {
			cout << "━";
		}
	}
	//Display H pattern of given odd size
	void h_pattern(int size) {
		if (size < 0 ||
			size % 2 == 0) {
			return;
		}
		cout << "\n Size : " << size << " \n\n";
		int i = 0;
		for (i = 0; i < size; i++) {
			this->space(2);
			cout << "┃";
			if (i == (size / 2)) {
				this->print_symbol(size);
			} else {
				this->space(size);
			}
			cout << "┃";
			cout << "\n";
		}
	}
};
int main() {
	MyPattern obj =  MyPattern();
	//Test Cases
	obj.h_pattern(3);
	obj.h_pattern(5);
	obj.h_pattern(7);
	return 0;
}

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  Java Program
  Display H patterns
*/

public class MyPattern {

  /*Include Space of given size*/
  public void space(int size) {

    int counter = 0;

    for (counter = 0; counter < size; counter++) {

      //Add space
      System.out.print(" ");
    }
  }

  void print_symbol(int size) {
    int counter = 0;
    for (counter = 0; counter < size; counter++) {
      System.out.print("━");
    }
  }
  //Display H pattern of given odd size
  void h_pattern(int size) {
    if(size<0 || size%2==0)
    {
      return;
    }
    
    System.out.print("\n  Size : "+size+" \n\n");
    
    int i=0;

    for(i=0;i<size;i++){
      space(2);
      System.out.print("┃");
      
      if(i==(size/2))
      {
        print_symbol(size);
      }
      else
      {
        space(size);
      }
      System.out.print("┃");
      System.out.print("\n");
    } 
  }

  public static void main(String[] args) {

    MyPattern obj = new MyPattern();
    //Test Cases
    obj.h_pattern(3);
    obj.h_pattern(5);
    obj.h_pattern(7);
  }
}

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  C# Program
  Display H patterns
*/
using System;

public class MyPattern {
	/*Include Space of given size*/
	public void space(int size) {
		int counter = 0;
		for (counter = 0; counter < size; counter++) {
			Console.Write(" ");
		}
	}
	void print_symbol(int size) {
		int counter = 0;
		for (counter = 0; counter < size; counter++) {
			Console.Write("━");
		}
	}
	//Display H pattern of given odd size
	void h_pattern(int size) {
		if (size < 0 ||
			size % 2 == 0) {
			return;
		}
		Console.Write("\n Size : " + size + " \n\n");
		int i = 0;
		for (i = 0; i < size; i++) {
			space(2);
			Console.Write("┃");
			if (i == (size / 2)) {
				print_symbol(size);
			} else {
				space(size);
			}
			Console.Write("┃");
			Console.Write("\n");
		}
	}
	public static void Main(String[] args) {
		MyPattern obj = new MyPattern();
		obj.h_pattern(3);
		obj.h_pattern(5);
		obj.h_pattern(7);
	}
}

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
<?php
/*
  Php Program
  Display H patterns
*/
class MyPattern {
	/*Include Space of given size*/

	public 	function space($size) {
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++) {
			//Add space

			echo(" ");
		}
	}

	function print_symbol($size) {
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++) {
			echo("━");
		}
	}
	//Display H pattern of given odd size
	function h_pattern($size) {
		if ($size < 0 ||
			$size % 2 == 0) {
			return;
		}
		echo("\n Size : ". $size ." \n\n");
		$i = 0;
		for ($i = 0; $i < $size; $i++) {
			$this->space(2);
			echo("┃");
			if ($i == (intval($size / 2))) {
				$this->print_symbol($size);
			} else {
				$this->space($size);
			}
			echo("┃");
			echo("\n");
		}
	}
}

function main() {
	$obj = new MyPattern();
	//Test Cases

	$obj->h_pattern(3);
	$obj->h_pattern(5);
	$obj->h_pattern(7);

}
main();

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  Node Js Program
  Display H patterns
*/
class MyPattern {
	/*Include Space of given size*/
	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 H pattern of given odd size
	h_pattern(size) {
		if (size < 0 ||
			size % 2 == 0) {
			return;
		}

		process.stdout.write("\n Size : " + size + " \n\n");
		var i = 0;
		for (i = 0; i < size; i++) {
			this.space(2);
			process.stdout.write("┃");
			if (i == (parseInt(size / 2))) {
				this.print_symbol(size);
			} else {
				this.space(size);
			}

			process.stdout.write("┃");
			process.stdout.write("\n");
		}
	}
}

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

main();

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
#   Python 3 Program
#   Display H patterns

class MyPattern :
	# Include Space of given size
	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
		
	
	def h_pattern(self, size) :
		if (size < 0 or size % 2 == 0) :
			return
		
		print("\n Size : ", size ," \n\n", end = "")
		i = 0
		while (i < size) :
			self.space(2)
			print("┃", end = "")
			if (i == (int(size / 2))) :
				self.print_symbol(size)
			else :
				self.space(size)
			
			print("┃", end = "")
			print("\n", end = "")
			i += 1
		
	

def main() :
	obj = MyPattern()
	obj.h_pattern(3)
	obj.h_pattern(5)
	obj.h_pattern(7)


if __name__ == "__main__":
	main()

Output

 Size :  3

  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size :  5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size :  7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
#   Ruby Program
#   Display H patterns

class MyPattern 
	# Include Space of given size
	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
	def h_pattern(size) 
		if (size < 0 ||
			size % 2 == 0) 
			return
		end
		print("\n Size  :", size ," \n\n")
		i = 0
		while (i < size) 
			self.space(2)
			print("┃")
			if (i == (size / 2)) 
				self.print_symbol(size)
			else 
				self.space(size)
			end
			print("┃")
			print("\n")
			i += 1
		end
	end
end
def main() 
	obj = MyPattern.new()
	obj.h_pattern(3)
	obj.h_pattern(5)
	obj.h_pattern(7)
end
main()

Output

 Size  :3 

  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size  :5 

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size  :7 

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  Scala Program
  Display H patterns
*/
class MyPattern {
	/*Include Space of given size*/
	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;
		}
	}
	def h_pattern(size: Int): Unit = {
		if (size < 0 ||
			size % 2 == 0) {
			return;
		}
		print("\n Size : " + size + " \n\n");
		var i: Int = 0;
		while (i < size) {
			this.space(2);
			print("┃");

			if (i == ((size / 2).toInt)) {
				this.print_symbol(size);
			} else {
				this.space(size);
			}
			print("┃");
			print("\n");
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyPattern = new MyPattern();
		obj.h_pattern(3);
		obj.h_pattern(5);
		obj.h_pattern(7);
	}
}

Output

 Size : 3

  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size : 5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size : 7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃
/*
  Swift Program
  Display H patterns
*/
class MyPattern {
	/*Include Space of given size*/
	func space(_ size: Int) {
		var counter = 0;
		while (counter < size) {
			print(" ", terminator: "");
			counter += 1;
		}
	}
	func print_symbol(_ size: Int) {
		var counter = 0;
		while (counter < size) {
			print("━", terminator: "");
			counter += 1;
		}
	}
	func h_pattern(_ size: Int) {
		if (size < 0 ||
			size % 2 == 0) {
			return;
		}
		print("\n Size : ", size ," \n\n", terminator: "");
		var i = 0;
		while (i < size) {
			self.space(2);
			print("┃", terminator: "");
			if (i == (size / 2)) {
				self.print_symbol(size);
			} else {
				self.space(size);
			}
			print("┃", terminator: "");
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main() {
	let obj = MyPattern();
	obj.h_pattern(3);
	obj.h_pattern(5);
	obj.h_pattern(7);
}
main();

Output

 Size :  3

  ┃   ┃
  ┃━━━┃
  ┃   ┃

 Size :  5

  ┃     ┃
  ┃     ┃
  ┃━━━━━┃
  ┃     ┃
  ┃     ┃

 Size :  7

  ┃       ┃
  ┃       ┃
  ┃       ┃
  ┃━━━━━━━┃
  ┃       ┃
  ┃       ┃
  ┃       ┃

Output Explanation

The output shows the generated H pattern for different input sizes: 3, 5, and 7.

For size 3, the pattern consists of three rows. The middle row contains the horizontal line '━━━', aligned with the center of the pattern.

For size 5, the pattern consists of five rows. The middle row contains the horizontal line '━━━━━'.

For size 7, the pattern consists of seven rows. Again, the middle row contains the horizontal line '━━━━━━━', aligned with the center of the pattern.

Time Complexity

The time complexity of this code is O(size), where 'size' refers to the input size. Since we iterate through each row and perform a constant number of operations for each row, the time complexity is linear in terms of the input size.





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.

New Comment