Print the pattern with two hollow triangles
In this article, we will explore a C program to print a pattern consisting of two hollow triangles. We will provide an explanation of the problem, present a suitable example, provide an algorithm and pseudocode, and discuss the resultant output along with the time complexity of the code.
Introduction
The objective of this problem is to create a pattern with two hollow triangles using asterisks as the pattern elements. The pattern will have a top section, an intermediate layer, a bottom layer, and a bottom section.
Problem Statement
Given an input value 'n,' the program should print a pattern with two hollow triangles. The value of 'n' determines the size of the pattern and affects the number of asterisks in each row.
Example:
Let's consider two examples:
Example 1: Given n = 5
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
Example 2: Given n = 8
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
Algorithm and Pseudocode
The algorithm to print the pattern with two hollow triangles can be broken down into the following steps:
- Define a function to include spaces for creating indentation in the pattern.
- Define the main function to print the pattern.
- Print the top section of the pattern.
- Print the intermediate layer of the pattern.
- Print the bottom layer of the pattern.
- Print the bottom section of the pattern.
- Call the main function with different input values to test the pattern printing.
Below is the pseudocode for the program:
includeSpace(n):
for i from 0 to n-1:
print space
printPattern(n):
print "Given n: " + n
// Top Section
for i from 0 to n-1:
includeSpace((1 + n - i) * 2)
for j from 0 to i * 2:
print "✰ "
print new line
// Intermediate layer
for i from 1 to n:
includeSpace(4)
for j from 0 to i-1:
print "✰ "
includeSpace((n - i) * 4 - 2)
for j from 0 to i-1:
if (j + 1) != n:
print "✰ "
print new line
// Bottom layer
for i from n-1 to 1:
includeSpace(4)
for j from 0 to i-1:
print "✰ "
includeSpace((n - i) * 4 - 2)
for j from 0 to i-1:
if (j + 1) != n:
print "✰ "
print new line
// Bottom Section
for i from 1 to n:
includeSpace(2 + i * 2)
for j from 0 to (n - i) * 2:
print "✰ "
print new line
main():
printPattern(5)
printPattern(8)
The main function calls the printPattern function with different input values to generate the desired pattern.
Code Solution
// C program for
// Print the pattern with two hollow triangles
#include <stdio.h>
// This is display empty space of given length
void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
printf(" ");
}
}
void printPattern(int n)
{
printf("\n Given n : %d \n", n);
// Top Section
for (int i = 0; i < n; ++i)
{
includeSpace((1 + n - i) *2);
for (int j = 0; j <= i *2; ++j)
{
printf("✰ ");
}
printf("\n");
}
// Intermediate layer
for (int i = 1; i <= n; ++i)
{
includeSpace(4);
for (int j = 0; j < i; ++j)
{
printf("✰ ");
}
includeSpace((n - i) *4 - 2);
for (int j = 0; j < i; ++j)
{
if (j + 1 != n)
{
printf("✰ ");
}
}
printf("\n");
}
// Bottom layer
for (int i = n - 1; i > 0; --i)
{
includeSpace(4);
for (int j = 0; j < i; ++j)
{
printf("✰ ");
}
includeSpace((n - i) *4 - 2);
for (int j = 0; j < i; ++j)
{
if (j + 1 != n)
{
printf("✰ ");
}
}
printf("\n");
}
// Bottom Section
for (int i = 1; i <= n; ++i)
{
includeSpace(2 + i *2);
for (int j = 0; j <= (n - i) *2; ++j)
{
printf("✰ ");
}
printf("\n");
}
}
int main(int argc, char
const *argv[])
{
// Test
printPattern(5);
printPattern(8);
return 0;
}
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
/*
Java Program
Print the pattern with two hollow triangles
*/
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 printPattern(int n)
{
System.out.print("\n Given n : " + n + " \n");
// Top Section
for (int i = 0; i < n; ++i)
{
includeSpace((1 + n - i) * 2);
for (int j = 0; j <= i * 2; ++j)
{
System.out.print("✰ ");
}
System.out.print("\n");
}
// Intermediate layer
for (int i = 1; i <= n; ++i)
{
includeSpace(4);
for (int j = 0; j < i; ++j)
{
System.out.print("✰ ");
}
includeSpace((n - i) * 4 - 2);
for (int j = 0; j < i; ++j)
{
if (j + 1 != n)
{
System.out.print("✰ ");
}
}
System.out.print("\n");
}
// Bottom layer
for (int i = n - 1; i > 0; --i)
{
includeSpace(4);
for (int j = 0; j < i; ++j)
{
System.out.print("✰ ");
}
includeSpace((n - i) * 4 - 2);
for (int j = 0; j < i; ++j)
{
if (j + 1 != n)
{
System.out.print("✰ ");
}
}
System.out.print("\n");
}
// Bottom Section
for (int i = 1; i <= n; ++i)
{
includeSpace(2 + i * 2);
for (int j = 0; j <= (n - i) * 2; ++j)
{
System.out.print("✰ ");
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
Pattern task = new Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
}
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Print the pattern with two hollow triangles
*/
class Pattern
{
public:
// This is display empty space of given length
void includeSpace(int n)
{
for (int i = 0; i < n; ++i)
{
cout << " ";
}
}
void printPattern(int n)
{
cout << "\n Given n : " << n << " \n";
// Top Section
for (int i = 0; i < n; ++i)
{
this->includeSpace((1 + n - i) *2);
for (int j = 0; j <= i *2; ++j)
{
cout << "✰ ";
}
cout << "\n";
}
// Intermediate layer
for (int i = 1; i <= n; ++i)
{
this->includeSpace(4);
for (int j = 0; j < i; ++j)
{
cout << "✰ ";
}
this->includeSpace((n - i) *4 - 2);
for (int j = 0; j < i; ++j)
{
if (j + 1 != n)
{
cout << "✰ ";
}
}
cout << "\n";
}
// Bottom layer
for (int i = n - 1; i > 0; --i)
{
this->includeSpace(4);
for (int j = 0; j < i; ++j)
{
cout << "✰ ";
}
this->includeSpace((n - i) *4 - 2);
for (int j = 0; j < i; ++j)
{
if (j + 1 != n)
{
cout << "✰ ";
}
}
cout << "\n";
}
// Bottom Section
for (int i = 1; i <= n; ++i)
{
this->includeSpace(2 + i *2);
for (int j = 0; j <= (n - i) *2; ++j)
{
cout << "✰ ";
}
cout << "\n";
}
}
};
int main()
{
Pattern *task = new Pattern();
// Test
task->printPattern(5);
task->printPattern(8);
return 0;
}
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
// Include namespace system
using System;
/*
Csharp Program
Print the pattern with two hollow triangles
*/
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 printPattern(int n)
{
Console.Write("\n Given n : " + n + " \n");
// Top Section
for (int i = 0; i < n; ++i)
{
this.includeSpace((1 + n - i) * 2);
for (int j = 0; j <= i * 2; ++j)
{
Console.Write("✰ ");
}
Console.Write("\n");
}
// Intermediate layer
for (int i = 1; i <= n; ++i)
{
this.includeSpace(4);
for (int j = 0; j < i; ++j)
{
Console.Write("✰ ");
}
this.includeSpace((n - i) * 4 - 2);
for (int j = 0; j < i; ++j)
{
if (j + 1 != n)
{
Console.Write("✰ ");
}
}
Console.Write("\n");
}
// Bottom layer
for (int i = n - 1; i > 0; --i)
{
this.includeSpace(4);
for (int j = 0; j < i; ++j)
{
Console.Write("✰ ");
}
this.includeSpace((n - i) * 4 - 2);
for (int j = 0; j < i; ++j)
{
if (j + 1 != n)
{
Console.Write("✰ ");
}
}
Console.Write("\n");
}
// Bottom Section
for (int i = 1; i <= n; ++i)
{
this.includeSpace(2 + i * 2);
for (int j = 0; j <= (n - i) * 2; ++j)
{
Console.Write("✰ ");
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
Pattern task = new Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
}
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
package main
import "fmt"
/*
Go Program
Print the pattern with two hollow triangles
*/
type Pattern struct {}
func getPattern() * Pattern {
var me *Pattern = &Pattern {}
return me
}
// This is display empty space of given length
func(this Pattern) includeSpace(n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ")
}
}
func(this Pattern) printPattern(n int) {
fmt.Print("\n Given n : ", n, " \n")
// Top Section
for i := 0 ; i < n ; i++ {
this.includeSpace((1 + n - i) * 2)
for j := 0 ; j <= i * 2 ; j++ {
fmt.Print("✰ ")
}
fmt.Print("\n")
}
// Intermediate layer
for i := 1 ; i <= n ; i++ {
this.includeSpace(4)
for j := 0 ; j < i ; j++ {
fmt.Print("✰ ")
}
this.includeSpace((n - i) * 4 - 2)
for j := 0 ; j < i ; j++ {
if j + 1 != n {
fmt.Print("✰ ")
}
}
fmt.Print("\n")
}
// Bottom layer
for i := n - 1 ; i > 0 ; i-- {
this.includeSpace(4)
for j := 0 ; j < i ; j++ {
fmt.Print("✰ ")
}
this.includeSpace((n - i) * 4 - 2)
for j := 0 ; j < i ; j++ {
if j + 1 != n {
fmt.Print("✰ ")
}
}
fmt.Print("\n")
}
// Bottom Section
for i := 1 ; i <= n ; i++ {
this.includeSpace(2 + i * 2)
for j := 0 ; j <= (n - i) * 2 ; j++ {
fmt.Print("✰ ")
}
fmt.Print("\n")
}
}
func main() {
var task * Pattern = getPattern()
// Test
task.printPattern(5)
task.printPattern(8)
}
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
<?php
/*
Php Program
Print the pattern with two hollow triangles
*/
class Pattern
{
// This is display empty space of given length
public function includeSpace($n)
{
for ($i = 0; $i < $n; ++$i)
{
echo(" ");
}
}
public function printPattern($n)
{
echo("\n Given n : ".$n." \n");
// Top Section
for ($i = 0; $i < $n; ++$i)
{
$this->includeSpace((1 + $n - $i) * 2);
for ($j = 0; $j <= $i * 2; ++$j)
{
echo("✰ ");
}
echo("\n");
}
// Intermediate layer
for ($i = 1; $i <= $n; ++$i)
{
$this->includeSpace(4);
for ($j = 0; $j < $i; ++$j)
{
echo("✰ ");
}
$this->includeSpace(($n - $i) * 4 - 2);
for ($j = 0; $j < $i; ++$j)
{
if ($j + 1 != $n)
{
echo("✰ ");
}
}
echo("\n");
}
// Bottom layer
for ($i = $n - 1; $i > 0; --$i)
{
$this->includeSpace(4);
for ($j = 0; $j < $i; ++$j)
{
echo("✰ ");
}
$this->includeSpace(($n - $i) * 4 - 2);
for ($j = 0; $j < $i; ++$j)
{
if ($j + 1 != $n)
{
echo("✰ ");
}
}
echo("\n");
}
// Bottom Section
for ($i = 1; $i <= $n; ++$i)
{
$this->includeSpace(2 + $i * 2);
for ($j = 0; $j <= ($n - $i) * 2; ++$j)
{
echo("✰ ");
}
echo("\n");
}
}
}
function main()
{
$task = new Pattern();
// Test
$task->printPattern(5);
$task->printPattern(8);
}
main();
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
/*
Node JS Program
Print the pattern with two hollow triangles
*/
class Pattern
{
// This is display empty space of given length
includeSpace(n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" ");
}
}
printPattern(n)
{
process.stdout.write("\n Given n : " + n + " \n");
// Top Section
for (var i = 0; i < n; ++i)
{
this.includeSpace((1 + n - i) * 2);
for (var j = 0; j <= i * 2; ++j)
{
process.stdout.write("✰ ");
}
process.stdout.write("\n");
}
// Intermediate layer
for (var i = 1; i <= n; ++i)
{
this.includeSpace(4);
for (var j = 0; j < i; ++j)
{
process.stdout.write("✰ ");
}
this.includeSpace((n - i) * 4 - 2);
for (var j = 0; j < i; ++j)
{
if (j + 1 != n)
{
process.stdout.write("✰ ");
}
}
process.stdout.write("\n");
}
// Bottom layer
for (var i = n - 1; i > 0; --i)
{
this.includeSpace(4);
for (var j = 0; j < i; ++j)
{
process.stdout.write("✰ ");
}
this.includeSpace((n - i) * 4 - 2);
for (var j = 0; j < i; ++j)
{
if (j + 1 != n)
{
process.stdout.write("✰ ");
}
}
process.stdout.write("\n");
}
// Bottom Section
for (var i = 1; i <= n; ++i)
{
this.includeSpace(2 + i * 2);
for (var j = 0; j <= (n - i) * 2; ++j)
{
process.stdout.write("✰ ");
}
process.stdout.write("\n");
}
}
}
function main()
{
var task = new Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
main();
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
# Python 3 Program
# Print the pattern with two hollow triangles
class Pattern :
# This is display empty space of given length
def includeSpace(self, n) :
i = 0
while (i < n) :
print(" ", end = "")
i += 1
def printPattern(self, n) :
print("\n Given n : ", n ," ")
i = 0
# Top Section
while (i < n) :
self.includeSpace((1 + n - i) * 2)
j = 0
while (j <= i * 2) :
print("✰ ", end = "")
j += 1
print(end = "\n")
i += 1
i = 1
# Intermediate layer
while (i <= n) :
self.includeSpace(4)
j = 0
while (j < i) :
print("✰ ", end = "")
j += 1
self.includeSpace((n - i) * 4 - 2)
j = 0
while (j < i) :
if (j + 1 != n) :
print("✰ ", end = "")
j += 1
print(end = "\n")
i += 1
i = n - 1
# Bottom layer
while (i > 0) :
self.includeSpace(4)
j = 0
while (j < i) :
print("✰ ", end = "")
j += 1
self.includeSpace((n - i) * 4 - 2)
j = 0
while (j < i) :
if (j + 1 != n) :
print("✰ ", end = "")
j += 1
print(end = "\n")
i -= 1
i = 1
# Bottom Section
while (i <= n) :
self.includeSpace(2 + i * 2)
j = 0
while (j <= (n - i) * 2) :
print("✰ ", end = "")
j += 1
print(end = "\n")
i += 1
def main() :
task = Pattern()
# Test
task.printPattern(5)
task.printPattern(8)
if __name__ == "__main__": main()
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
# Ruby Program
# Print the pattern with two hollow triangles
class Pattern
# This is display empty space of given length
def includeSpace(n)
i = 0
while (i < n)
print(" ")
i += 1
end
end
def printPattern(n)
print("\n Given n : ", n ," \n")
i = 0
# Top Section
while (i < n)
self.includeSpace((1 + n - i) * 2)
j = 0
while (j <= i * 2)
print("✰ ")
j += 1
end
print("\n")
i += 1
end
i = 1
# Intermediate layer
while (i <= n)
self.includeSpace(4)
j = 0
while (j < i)
print("✰ ")
j += 1
end
self.includeSpace((n - i) * 4 - 2)
j = 0
while (j < i)
if (j + 1 != n)
print("✰ ")
end
j += 1
end
print("\n")
i += 1
end
i = n - 1
# Bottom layer
while (i > 0)
self.includeSpace(4)
j = 0
while (j < i)
print("✰ ")
j += 1
end
self.includeSpace((n - i) * 4 - 2)
j = 0
while (j < i)
if (j + 1 != n)
print("✰ ")
end
j += 1
end
print("\n")
i -= 1
end
i = 1
# Bottom Section
while (i <= n)
self.includeSpace(2 + i * 2)
j = 0
while (j <= (n - i) * 2)
print("✰ ")
j += 1
end
print("\n")
i += 1
end
end
end
def main()
task = Pattern.new()
# Test
task.printPattern(5)
task.printPattern(8)
end
main()
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
/*
Scala Program
Print the pattern with two hollow triangles
*/
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 printPattern(n: Int): Unit = {
print("\n Given n : " + n + " \n");
var i: Int = 0;
// Top Section
while (i < n)
{
includeSpace((1 + n - i) * 2);
var j: Int = 0;
while (j <= i * 2)
{
print("✰ ");
j += 1;
}
print("\n");
i += 1;
}
i = 1;
// Intermediate layer
while (i <= n)
{
includeSpace(4);
var j: Int = 0;
while (j < i)
{
print("✰ ");
j += 1;
}
includeSpace((n - i) * 4 - 2);
j = 0;
while (j < i)
{
if (j + 1 != n)
{
print("✰ ");
}
j += 1;
}
print("\n");
i += 1;
}
i = n - 1;
// Bottom layer
while (i > 0)
{
includeSpace(4);
var j: Int = 0;
while (j < i)
{
print("✰ ");
j += 1;
}
includeSpace((n - i) * 4 - 2);
j = 0;
while (j < i)
{
if (j + 1 != n)
{
print("✰ ");
}
j += 1;
}
print("\n");
i -= 1;
}
i = 1;
// Bottom Section
while (i <= n)
{
includeSpace(2 + i * 2);
var j: Int = 0;
while (j <= (n - i) * 2)
{
print("✰ ");
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pattern = new Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
}
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
/*
Swift 4 Program
Print the pattern with two hollow triangles
*/
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 printPattern(_ n: Int)
{
print("\n Given n : ", n ," ");
var i: Int = 0;
// Top Section
while (i < n)
{
self.includeSpace((1 + n - i) * 2);
var j: Int = 0;
while (j <= i * 2)
{
print("✰ ", terminator: "");
j += 1;
}
print(terminator: "\n");
i += 1;
}
i = 1;
// Intermediate layer
while (i <= n)
{
self.includeSpace(4);
var j: Int = 0;
while (j < i)
{
print("✰ ", terminator: "");
j += 1;
}
self.includeSpace((n - i) * 4 - 2);
j = 0;
while (j < i)
{
if (j + 1 != n)
{
print("✰ ", terminator: "");
}
j += 1;
}
print(terminator: "\n");
i += 1;
}
i = n - 1;
// Bottom layer
while (i > 0)
{
self.includeSpace(4);
var j: Int = 0;
while (j < i)
{
print("✰ ", terminator: "");
j += 1;
}
self.includeSpace((n - i) * 4 - 2);
j = 0;
while (j < i)
{
if (j + 1 != n)
{
print("✰ ", terminator: "");
}
j += 1;
}
print(terminator: "\n");
i -= 1;
}
i = 1;
// Bottom Section
while (i <= n)
{
self.includeSpace(2 + i * 2);
var j: Int = 0;
while (j <= (n - i) * 2)
{
print("✰ ", terminator: "");
j += 1;
}
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
let task: Pattern = Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
main();
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
/*
Kotlin Program
Print the pattern with two hollow triangles
*/
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 printPattern(n: Int): Unit
{
print("\n Given n : " + n + " \n");
var i: Int = 0;
// Top Section
while (i < n)
{
this.includeSpace((1 + n - i) * 2);
var j: Int = 0;
while (j <= i * 2)
{
print("✰ ");
j += 1;
}
print("\n");
i += 1;
}
i = 1;
// Intermediate layer
while (i <= n)
{
this.includeSpace(4);
var j: Int = 0;
while (j < i)
{
print("✰ ");
j += 1;
}
this.includeSpace((n - i) * 4 - 2);
j = 0;
while (j < i)
{
if (j + 1 != n)
{
print("✰ ");
}
j += 1;
}
print("\n");
i += 1;
}
i = n - 1;
// Bottom layer
while (i > 0)
{
this.includeSpace(4);
var j: Int = 0;
while (j < i)
{
print("✰ ");
j += 1;
}
this.includeSpace((n - i) * 4 - 2);
j = 0;
while (j < i)
{
if (j + 1 != n)
{
print("✰ ");
}
j += 1;
}
print("\n");
i -= 1;
}
i = 1;
// Bottom Section
while (i <= n)
{
this.includeSpace(2 + i * 2);
var j: Int = 0;
while (j <= (n - i) * 2)
{
print("✰ ");
j += 1;
}
print("\n");
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Pattern = Pattern();
// Test
task.printPattern(5);
task.printPattern(8);
}
Output
Given n : 5
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
Given n : 8
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
The pattern consists of two hollow triangles, one on top of the other. The number of rows in each triangle is determined by the input value 'n.' The pattern is created using asterisks ('✰') as the pattern elements, with appropriate indentation and spacing.
Time Complexity
The time complexity of this code is O(n^2), where 'n' represents the input value. This is because there are nested loops that iterate up to 'n' in different sections of the code. As the input value increases, the number of iterations in each loop increases quadratically, resulting in a quadratic time complexity.
Finally
In this article, we explored a C program to print a pattern with two hollow triangles using asterisks as pattern elements. We provided an explanation of the problem, presented examples, discussed the algorithm and pseudocode, and explained the resultant output. The time complexity of the code was also analyzed. By understanding and implementing this program, you can create visually appealing patterns using nested loops and proper indentation.
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