Print the pattern with two hollow triangles
Here given code implementation process.
// 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
✰
✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰
✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰ ✰ ✰
✰ ✰ ✰ ✰ ✰
✰ ✰ ✰
✰
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