Print hollow triangle

Here given code implementation process.

//  C program for
//  Print hollow triangle 
#include <stdio.h>

// This is display empty space of given length
void includeSpace(int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf(" ");
	}
}
void printTriangle(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");
	}
}
int main(int argc, char const *argv[])
{
	// Test
	printTriangle(7);
	printTriangle(12);
	return 0;
}

Output

Given height : 7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height : 12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
/*
    Java Program
    Print hollow triangle 
*/
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 printTriangle(int height)
	{
		System.out.println("\nGiven height : " + height);
		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");
		}
	}
	public static void main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printTriangle(7);
		task.printTriangle(12);
	}
}

Output

Given height : 7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height : 12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Print hollow triangle 
*/
class Pattern
{
	public:
		// This is display empty space of given length
		void includeSpace(int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " ";
			}
		}
	void printTriangle(int height)
	{
		cout << "\nGiven height : " << height << endl;
		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";
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printTriangle(7);
	task->printTriangle(12);
	return 0;
}

Output

Given height : 7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height : 12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
// Include namespace system
using System;
/*
    Csharp Program
    Print hollow triangle 
*/
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 printTriangle(int height)
	{
		Console.WriteLine("\nGiven height : " + height);
		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");
		}
	}
	public static void Main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printTriangle(7);
		task.printTriangle(12);
	}
}

Output

Given height : 7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height : 12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
package main
import "fmt"
/*
    Go Program
    Print hollow triangle 
*/

// This is display empty space of given length
func includeSpace(n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ")
	}
}
func printTriangle(height int) {
	fmt.Println("\nGiven height : ", height)
	for i := 0 ; i < height ; i++ {
		for j := height ; j > i ; j-- {
			fmt.Print("*")
		}
		// Include space
		includeSpace(i * 2)
		for k := height ; k > i ; k-- {
			fmt.Print("*")
		}
		fmt.Print("\n")
	}
}
func main() {
	
	// Test
	printTriangle(7)
	printTriangle(12)
}

Output

Given height : 7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height : 12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
<?php
/*
    Php Program
    Print hollow triangle 
*/
class Pattern
{
	// This is display empty space of given length
	public	function includeSpace($n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ");
		}
	}
	public	function printTriangle($height)
	{
		echo("\nGiven height : ".$height.
			"\n");
		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");
		}
	}
}

function main()
{
	$task = new Pattern();
	// Test
	$task->printTriangle(7);
	$task->printTriangle(12);
}
main();

Output

Given height : 7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height : 12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
/*
    Node JS Program
    Print hollow triangle 
*/
class Pattern
{
	// This is display empty space of given length
	includeSpace(n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" ");
		}
	}
	printTriangle(height)
	{
		console.log("\nGiven height : " + height);
		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");
		}
	}
}

function main()
{
	var task = new Pattern();
	// Test
	task.printTriangle(7);
	task.printTriangle(12);
}
main();

Output

Given height : 7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height : 12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
#    Python 3 Program
#    Print hollow triangle 
class Pattern :
	#  This is display empty space of given length
	def includeSpace(self, n) :
		i = 0
		while (i < n) :
			print(" ", end = "")
			i += 1
		
	
	def printTriangle(self, height) :
		print("\nGiven height : ", height)
		i = 0
		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
		
	

def main() :
	task = Pattern()
	#  Test
	task.printTriangle(7)
	task.printTriangle(12)

if __name__ == "__main__": main()

Output

Given height :  7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height :  12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
#    Ruby Program
#    Print hollow triangle 
class Pattern 
	#  This is display empty space of given length
	def includeSpace(n) 
		i = 0
		while (i < n) 
			print(" ")
			i += 1
		end

	end

	def printTriangle(height) 
		print("\nGiven height : ", height, "\n")
		i = 0
		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

	end

end

def main() 
	task = Pattern.new()
	#  Test
	task.printTriangle(7)
	task.printTriangle(12)
end

main()

Output

Given height : 7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height : 12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
/*
    Scala Program
    Print hollow triangle 
*/
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 printTriangle(height: Int): Unit = {
		println("\nGiven height : " + height);
		var i: Int = 0;
		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;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pattern = new Pattern();
		// Test
		task.printTriangle(7);
		task.printTriangle(12);
	}
}

Output

Given height : 7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height : 12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
/*
    Swift 4 Program
    Print hollow triangle 
*/
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 printTriangle(_ height: Int)
	{
		print("\nGiven height : ", height);
		var i: Int = 0;
		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;
		}
	}
}
func main()
{
	let task: Pattern = Pattern();
	// Test
	task.printTriangle(7);
	task.printTriangle(12);
}
main();

Output

Given height :  7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height :  12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
/*
    Kotlin Program
    Print hollow triangle 
*/
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 printTriangle(height: Int): Unit
	{
		println("\nGiven height : " + height);
		var i: Int = 0;
		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;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pattern = Pattern();
	// Test
	task.printTriangle(7);
	task.printTriangle(12);
}

Output

Given height : 7
**************
******  ******
*****    *****
****      ****
***        ***
**          **
*            *

Given height : 12
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *


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