Print triangle separated pattern
Here given code implementation process.
// 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
╲*╱
*╳*
╱*╲
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