Prerequisite : Loops, If Else Statement
1. Hollow pyramid/triangle pattern
The pattern is similar to pyramid pattern. The only difference is, we will replace all internal ‘#’ characters by space character and we will 2*N-1 (N = number of rows in pattern) ‘#’ characters in last row.
Examples:
Input rows: 6
Output:
#
# #
# #
# #
# #
# #
###########
C++
#include <iostream>
using namespace std;
void printPattern(int);
int main()
{
int n = 6;
printPattern(n);
}
void printPattern(int n)
{
int i, j, k = 0;
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++) {
cout << " ";
}
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
cout << "#";
else
cout << " ";
k++;
;
}
k = 0;
cout << endl;
}
for (i = 0; i < 2 * n - 1; i++) {
cout << "#";
}
}
|
Java
class GFG{
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
static void printPattern(int n)
{
int i, j, k = 0;
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++) {
System.out.print(" ");
}
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
System.out.print("#");
else
System.out.print(" ");
k++;
;
}
k = 0;
System.out.println();
}
for (i = 0; i < 2 * n - 1; i++) {
System.out.print("#");
}
}
}
|
Python
def printPattern( n) :
k = 0
for i in range(1,n+1) :
for j in range(i,n) :
print(' ', end='')
while (k != (2 * i - 1)) :
if (k == 0 or k == 2 * i - 2) :
print('#', end='')
else :
print(' ', end ='')
k = k + 1
k = 0;
print ("")
for i in range(0, 2 * n -1) :
print ('#', end = '')
n = 6
printPattern(n)
|
PHP
<?php
function printPattern($n)
{
$k = 0;
for ($i = 1; $i <= $n; $i++)
{
for ($j = $i; $j < $n; $j++)
{
echo " ";
}
while ($k != (2 * $i - 1))
{
if ($k == 0 || $k == 2 *
$i - 2)
echo "#";
else
echo " ";
$k++;
}
$k = 0;
echo "\n";
}
for ($i = 0; $i < 2 * $n - 1; $i++)
{
echo "#";
}
}
$n = 6;
printPattern($n);
?>
|
Javascript
<script>
var n = 6;
printPattern(n);
function printPattern(n)
{
var i, j, k = 0;
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++)
{
document.write(" ");
}
while (k != 2 * i - 1)
{
if (k == 0 || k == 2 * i - 2)
document.write("#");
else
document.write(" ");
k++;
}
k = 0;
document.write("<br>");
}
for (i = 0; i < 2 * n - 1; i++)
{
document.write("#");
}
}
</script>
|
Output:
#
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our
Data Structures in C course to start learning today.