Skip to main content

Print triangle separated pattern

In this article, we will explore the problem of printing a triangle separated pattern. We will provide an explanation of the problem statement, a suitable example, an algorithm and pseudocode with explanations, and an explanation of the resultant output. The code provided in the question will not be included in this article. Let's dive in!

Problem Statement

The problem is to print a triangle separated pattern of a given size. The pattern consists of asterisks (*) and diagonal lines (╲ and ╱). The triangle pattern is separated by a central element, which is represented by a cross symbol (╳) when the size is odd. The pattern is symmetric with respect to this central element.

Example:

For a size of 5, the pattern looks like this:

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Complete Pseudocode

function printPattern(size):
    if size <= 2:
        return
    print "Size: ", size
    for i = 0 to size-1:
        for j = 0 to size-1:
            if size is odd and i = size/2 and i = j:
                print "╳"
            else if j = i:
                print "╲"
            else if size - 1 - i = j:
                print "╱"
            else:
                print "*"
        print newline

function main():
    printPattern(5)
    printPattern(7)
    printPattern(10)
    printPattern(3)

Algorithm

1. Start.
2. Accept the size of the pattern.
3. If the size is less than or equal to 2, return.
4. Print the size of the pattern.
5. Initialize variables i and j for controlling the row and column operations.
6. Iterate over the rows from 0 to size-1.
7. Iterate over the columns from 0 to size-1.
8. Check if the size is odd and the current position is the middle element.
   - If true, print the cross symbol (╳).
9. Check if the current position is on the diagonal (j == i).
   - If true, print the diagonal line (╲).
10. Check if the current position is on the opposite diagonal (size - 1 - i == j).
    - If true, print the diagonal line (╱).
11. If none of the above conditions are satisfied, print an asterisk (*).
12. Print a new line.
13. Repeat steps 6 to 12 until all rows and columns are printed.
14. End.

Code Solution

// C Program 
// Print triangle separated pattern
#include <stdio.h>


void printPattern(int size)
{
    if (size <= 2)
    {
        return;
    }
    printf("\nSize : %d  \n\n", size);
    // Loop controlling variables
    int i = 0, j = 0;
    // Loop, which is control the printing row operations
    for (i = 0; i < size; ++i)
    {
        // Loop, which is control the printing column operations
        for (j = 0; j < size; ++j)
        {
            
            if(size%2 != 0 && i == size/2 && i == j)
            {
                // Middle element of Even size
                printf("╳");
            }
            else if ( j == i)
            {
                printf("╲");
            }
            else if(size - 1 - i == j)
            {
                printf("╱");
            }
            else
            {
                // Include star
                printf("*");
            }
        }
        // Include new line
        printf("\n");
    }
}
int main()
{
    /*
    Example Size : 5  
    ----------------
        ╲☀☀☀╱
        ☀╲☀╱☀
        ☀☀╳☀☀
        ☀╱☀╲☀
        ╱☀☀☀╲
    ----------------
    */
    // Simple test
    printPattern(5);
    printPattern(7);
    printPattern(10);
    printPattern(3);
    return 0;
}

input

Size : 5

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size : 7

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size : 10

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size : 3

╲*╱
*╳*
╱*╲
/*
    Java Program
    Print triangle separated pattern
*/
public class TrianglePattern
{
	public void printPattern(int size)
	{
		if (size <= 2)
		{
			return;
		}
		System.out.print("\nSize : " + size + " \n\n");
		// Loop controlling variables
		int i = 0, j = 0;
		// Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			// Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				if (size % 2 != 0 && i == size / 2 && i == j)
				{
					// Middle element of Even size
					System.out.print("╳");
				}
				else if (j == i)
				{
					System.out.print("╲");
				}
				else if (size - 1 - i == j)
				{
					System.out.print("╱");
				}
				else
				{
					// Include star
					System.out.print("*");
				}
			}
			// Include new line
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		TrianglePattern task = new TrianglePattern();
		/*
		    Example Size : 5  
		    ----------------
		        ╲☀☀☀╱
		        ☀╲☀╱☀
		        ☀☀╳☀☀
		        ☀╱☀╲☀
		        ╱☀☀☀╲
		    ----------------
		*/
		// Simple test
		task.printPattern(5);
		task.printPattern(7);
		task.printPattern(10);
		task.printPattern(3);
	}
}

input

Size : 5

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size : 7

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size : 10

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size : 3

╲*╱
*╳*
╱*╲
// Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Print triangle separated pattern
*/

class TrianglePattern
{
	public: void printPattern(int size)
	{
		if (size <= 2)
		{
			return;
		}
		cout << "\nSize : " << size << " \n\n";
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			// Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				if (size % 2 != 0 && i == size / 2 && i == j)
				{
					// Middle element of Even size
					cout << "╳";
				}
				else if (j == i)
				{
					cout << "╲";
				}
				else if (size - 1 - i == j)
				{
					cout << "╱";
				}
				else
				{
					// Include star
					cout << "*";
				}
			}
			// Include new line
			cout << "\n";
		}
	}
};
int main()
{
	TrianglePattern *task = new TrianglePattern();
	/*
			    Example Size : 5  
			    ----------------
			        ╲☀☀☀╱
			        ☀╲☀╱☀
			        ☀☀╳☀☀
			        ☀╱☀╲☀
			        ╱☀☀☀╲
			    ----------------
			*/
	// Simple test
	task->printPattern(5);
	task->printPattern(7);
	task->printPattern(10);
	task->printPattern(3);
	return 0;
}

input

Size : 5

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size : 7

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size : 10

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size : 3

╲*╱
*╳*
╱*╲
// Include namespace system
using System;
/*
    Csharp Program
    Print triangle separated pattern
*/
public class TrianglePattern
{
	public void printPattern(int size)
	{
		if (size <= 2)
		{
			return;
		}
		Console.Write("\nSize : " + size + " \n\n");
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			// Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				if (size % 2 != 0 && i == size / 2 && i == j)
				{
					// Middle element of Even size
					Console.Write("╳");
				}
				else if (j == i)
				{
					Console.Write("╲");
				}
				else if (size - 1 - i == j)
				{
					Console.Write("╱");
				}
				else
				{
					// Include star
					Console.Write("*");
				}
			}
			// Include new line
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		TrianglePattern task = new TrianglePattern();
		/*
		    Example Size : 5  
		    ----------------
		        ╲☀☀☀╱
		        ☀╲☀╱☀
		        ☀☀╳☀☀
		        ☀╱☀╲☀
		        ╱☀☀☀╲
		    ----------------
		*/
		// Simple test
		task.printPattern(5);
		task.printPattern(7);
		task.printPattern(10);
		task.printPattern(3);
	}
}

input

Size : 5

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size : 7

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size : 10

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size : 3

╲*╱
*╳*
╱*╲
<?php
/*
    Php Program
    Print triangle separated pattern
*/
class TrianglePattern
{
	public	function printPattern($size)
	{
		if ($size <= 2)
		{
			return;
		}
		echo("\nSize : ".$size.
			" \n\n");
		// Loop controlling variables
		$i = 0;
		$j = 0;
		// Loop, which is control the printing row operations
		for ($i = 0; $i < $size; ++$i)
		{
			// Loop, which is control the printing column operations
			for ($j = 0; $j < $size; ++$j)
			{
				if ($size % 2 != 0 && $i == (int)($size / 2) && $i == $j)
				{
					// Middle element of Even size
					echo("╳");
				}
				else if ($j == $i)
				{
					echo("╲");
				}
				else if ($size - 1 - $i == $j)
				{
					echo("╱");
				}
				else
				{
					// Include star
					echo("*");
				}
			}
			// Include new line
			echo("\n");
		}
	}
}

function main()
{
	$task = new TrianglePattern();
	/*
	    Example Size : 5  
	    ----------------
	        ╲☀☀☀╱
	        ☀╲☀╱☀
	        ☀☀╳☀☀
	        ☀╱☀╲☀
	        ╱☀☀☀╲
	    ----------------
	*/
	// Simple test
	$task->printPattern(5);
	$task->printPattern(7);
	$task->printPattern(10);
	$task->printPattern(3);
}
main();

input

Size : 5

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size : 7

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size : 10

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size : 3

╲*╱
*╳*
╱*╲
/*
    Node JS Program
    Print triangle separated pattern
*/
class TrianglePattern
{
	printPattern(size)
	{
		if (size <= 2)
		{
			return;
		}
		process.stdout.write("\nSize : " + size + " \n\n");
		// Loop controlling variables
		var i = 0;
		var j = 0;
		// Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			// Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				if (size % 2 != 0 && i == parseInt(size / 2) && i == j)
				{
					// Middle element of Even size
					process.stdout.write("╳");
				}
				else if (j == i)
				{
					process.stdout.write("╲");
				}
				else if (size - 1 - i == j)
				{
					process.stdout.write("╱");
				}
				else
				{
					// Include star
					process.stdout.write("*");
				}
			}
			// Include new line
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var task = new TrianglePattern();
	/*
	    Example Size : 5  
	    ----------------
	        ╲☀☀☀╱
	        ☀╲☀╱☀
	        ☀☀╳☀☀
	        ☀╱☀╲☀
	        ╱☀☀☀╲
	    ----------------
	*/
	// Simple test
	task.printPattern(5);
	task.printPattern(7);
	task.printPattern(10);
	task.printPattern(3);
}
main();

input

Size : 5

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size : 7

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size : 10

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size : 3

╲*╱
*╳*
╱*╲
#    Python 3 Program
#    Print triangle separated pattern
class TrianglePattern :
	def printPattern(self, size) :
		if (size <= 2) :
			return
		
		print("\nSize : ", size ," \n")
		#  Loop controlling variables
		i = 0
		j = 0
		#  Loop, which is control the printing row operations
		while (i < size) :
			#  Loop, which is control the printing column operations
			j = 0
			while (j < size) :
				if (size % 2 != 0 and i == int(size / 2) and i == j) :
					#  Middle element of Even size
					print("╳", end = "")
				elif (j == i) :
					print("╲", end = "")
				elif (size - 1 - i == j) :
					print("╱", end = "")
				else :
					#  Include star
					print("*", end = "")
				
				j += 1
			
			#  Include new line
			print(end = "\n")
			i += 1
		
	

def main() :
	task = TrianglePattern()
	#    Example Size : 5  
	#    ----------------
	#        ╲☀☀☀╱
	#        ☀╲☀╱☀
	#        ☀☀╳☀☀
	#        ☀╱☀╲☀
	#        ╱☀☀☀╲
	#    ----------------
	#  Simple test
	task.printPattern(5)
	task.printPattern(7)
	task.printPattern(10)
	task.printPattern(3)

if __name__ == "__main__": main()

input

Size :  5

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size :  7

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size :  10

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size :  3

╲*╱
*╳*
╱*╲
#    Ruby Program
#    Print triangle separated pattern
class TrianglePattern 
	def printPattern(size) 
		if (size <= 2) 
			return
		end

		print("\nSize : ", size ," \n\n")
		#  Loop controlling variables
		i = 0
		j = 0
		#  Loop, which is control the printing row operations
		while (i < size) 
			#  Loop, which is control the printing column operations
			j = 0
			while (j < size) 
				if (size % 2 != 0 && i == size / 2 && i == j) 
					#  Middle element of Even size
					print("╳")
				elsif (j == i) 
					print("╲")
				elsif (size - 1 - i == j) 
					print("╱")
				else
					#  Include star
					print("*")
				end

				j += 1
			end

			#  Include new line
			print("\n")
			i += 1
		end

	end

end

def main() 
	task = TrianglePattern.new()
	#    Example Size : 5  
	#    ----------------
	#        ╲☀☀☀╱
	#        ☀╲☀╱☀
	#        ☀☀╳☀☀
	#        ☀╱☀╲☀
	#        ╱☀☀☀╲
	#    ----------------
	#  Simple test
	task.printPattern(5)
	task.printPattern(7)
	task.printPattern(10)
	task.printPattern(3)
end

main()

input

Size : 5 

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size : 7 

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size : 10 

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size : 3 

╲*╱
*╳*
╱*╲
/*
    Scala Program
    Print triangle separated pattern
*/
class TrianglePattern()
{
	def printPattern(size: Int): Unit = {
		if (size <= 2)
		{
			return;
		}
		print("\nSize : " + size + " \n\n");
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Loop, which is control the printing row operations
		while (i < size)
		{
          	j = 0;
			// Loop, which is control the printing column operations
		
			while (j < size)
			{
				if (size % 2 != 0 && i == size / 2 && i == j)
				{
					// Middle element of Even size
					print("╳");
				}
				else if (j == i)
				{
					print("╲");
				}
				else if (size - 1 - i == j)
				{
					print("╱");
				}
				else
				{
					// Include star
					print("*");
				}
				j += 1;
			}
			// Include new line
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: TrianglePattern = new TrianglePattern();
		/*
		    Example Size : 5  
		    ----------------
		        ╲☀☀☀╱
		        ☀╲☀╱☀
		        ☀☀╳☀☀
		        ☀╱☀╲☀
		        ╱☀☀☀╲
		    ----------------
		*/
		// Simple test
		task.printPattern(5);
		task.printPattern(7);
		task.printPattern(10);
		task.printPattern(3);
	}
}

input

Size : 5

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size : 7

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size : 10

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size : 3

╲*╱
*╳*
╱*╲
/*
    Swift 4 Program
    Print triangle separated pattern
*/
class TrianglePattern
{
	func printPattern(_ size: Int)
	{
		if (size <= 2)
		{
			return;
		}
		print("\nSize : ", size ," \n");
		// Loop controlling variables
		var i = 0;
		var j = 0;
		// Loop, which is control the printing row operations
		while (i < size)
		{
			// Loop, which is control the printing column operations
			j = 0;
			while (j < size)
			{
				if (size % 2  != 0 && i == size / 2 && i == j)
				{
					// Middle element of Even size
					print("╳", terminator: "");
				}
				else if (j == i)
				{
					print("╲", terminator: "");
				}
				else if (size - 1 - i == j)
				{
					print("╱", terminator: "");
				}
				else
				{
					// Include star
					print("*", terminator: "");
				}
				j += 1;
			}
			// Include new line
			print(terminator: "\n");
			i += 1;
		}
	}
}
func main()
{
	let task = TrianglePattern();
	/*
	    Example Size : 5  
	    ----------------
	        ╲☀☀☀╱
	        ☀╲☀╱☀
	        ☀☀╳☀☀
	        ☀╱☀╲☀
	        ╱☀☀☀╲
	    ----------------
	*/
	// Simple test
	task.printPattern(5);
	task.printPattern(7);
	task.printPattern(10);
	task.printPattern(3);
}
main();

input

Size :  5

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size :  7

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size :  10

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size :  3

╲*╱
*╳*
╱*╲
/*
    Kotlin Program
    Print triangle separated pattern
*/
class TrianglePattern
{
	fun printPattern(size: Int): Unit
	{
		if (size <= 2)
		{
			return;
		}
		print("\nSize : " + size + " \n\n");
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Loop, which is control the printing row operations
		while (i < size)
		{
			// Loop, which is control the printing column operations
			while (j < size)
			{
				if (size % 2 != 0 && i == size / 2 && i == j)
				{
					// Middle element of Even size
					print("╳");
				}
				else if (j == i)
				{
					print("╲");
				}
				else if (size - 1 - i == j)
				{
					print("╱");
				}
				else
				{
					// Include star
					print("*");
				}
				j += 1;
			}
			// Include new line
			print("\n");
			i += 1;
          	j = 0;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: TrianglePattern = TrianglePattern();
	/*
	    Example Size : 5  
	    ----------------
	        ╲☀☀☀╱
	        ☀╲☀╱☀
	        ☀☀╳☀☀
	        ☀╱☀╲☀
	        ╱☀☀☀╲
	    ----------------
	*/
	// Simple test
	task.printPattern(5);
	task.printPattern(7);
	task.printPattern(10);
	task.printPattern(3);
}

input

Size : 5

╲***╱
*╲*╱*
**╳**
*╱*╲*
╱***╲

Size : 7

╲*****╱
*╲***╱*
**╲*╱**
***╳***
**╱*╲**
*╱***╲*
╱*****╲

Size : 10

╲********╱
*╲******╱*
**╲****╱**
***╲**╱***
****╲╱****
****╱╲****
***╱**╲***
**╱****╲**
*╱******╲*
╱********╲

Size : 3

╲*╱
*╳*
╱*╲

The time complexity of the code is O(n^2), where n is the size of the pattern. This is because there are nested loops iterating over the rows and columns, resulting in a quadratic time complexity.

Finally, the code provided prints a triangle separated pattern of a given size. The pattern consists of asterisks and diagonal lines, with a central cross symbol for odd-sized patterns. The output showcases various pattern sizes and demonstrates the symmetrical nature of the pattern. The provided algorithm and pseudocode explain the logic behind the code, and the resultant output illustrates the expected patterns.





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