Skip to main content

Print H pattern

Here given code implementation process.

//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

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




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