9/3/2019 Print pyramid pattern in C and Java - Techie Delight
Techie Delight
Coding made easy
All Problems Array Tree Linked List DP Graph Backtracking Matrix
Heap D&C String Sorting Stack Queue Binary Puzzles IDE
Print pyramid pattern in C Custom Search
and Java
Write a C and Java program to print
pyramid pattern of stars. This post
covers below patterns formed by
pyramid –
Pattern 1: Pyramid
Pattern 2: Inverted Pyramid
Pattern 3: Hollow Pyramid
Browse
Pattern 4: Hollow Inverted Pyramid
Adobe Algorithm Amazon
BFS Binary Search Bit Hacks DFS FIFO
Google Greedy Hashing Intro JSON LCS
LIFO Maze Memoized Microsoft
Pattern 1: Pyramid Must Know Priority Queue Probability
Recursive Searching Sliding
Window Tabulation Tricky Trie
*
* * *
* *cookies.
This website uses * * By * using this site you agree to the use of cookies, our policies, copyright terms and
* * *Read
other conditions. * our
* Privacy
* * Policy
* * * * * * * * *
Close and accept
https://www.techiedelight.com/c-program-print-pyramid-pattern/ 1/10
9/3/2019 Print pyramid pattern in C and Java - Techie Delight
We have n rows. Here, 1st row will contain
(n-1) spaces followed by 1 star, 2nd row will
contain (n-2) spaces followed by 3 stars, 3rd
row will contain (n-3) spaces followed by 5
stars, and so on.. We can use nested loops to
print this pattern where outer loop
represents row number (say i) and inner
loop prints the space (n-i times) followed by
the star pattern (2*i – 1 times).
#include <stdio.h>
int main(void)
{
// n is number of rows
int n = 5;
int i, j, k;
// do for each row
for (i = 1; i <= n; i++)
{
// print space
for (j = i; j < n; j++)
printf(" "); Subscribe to posts
// print '*'
for (k = 1; k < 2*i; k++) Enter your email address to subscribe to new
printf("*");
posts and receive noti cations of new posts
// move to the next line by email.
printf("\n");
}
Email Address
return 0;
}
Subscribe
Download
Run Code
Java
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
other conditions. Read our Privacy Policy
1 class Pattern
2 {
3 public static void main(Strin Close and accept
https://www.techiedelight.com/c-program-print-pyramid-pattern/ 2/10
9/3/2019 Print pyramid pattern in C and Java - Techie Delight
4 {
5 // n is number of rows
6 final int n = 5;
7
8 // Do for each row
9 for (int i = 1; i <= n; i
10 {
11 // Print space
12 for (int j = i; j < n
13 System.out.print(
14 }
15
16 // Print '*'
17 for (int k = 1; k < 2
18 System.out.print(
19 }
20
21 // Move to the next l
22 System.out.print(Syst
23 }
24 }
25 }
Download
Run Code
Pattern 2: Inverted Pyramid
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Suppose we have n rows. First row will
contain 0 space followed by 2*n-1 stars, 2nd
row will contain 1 space followed by 2*n-3
stars, 3rd row will contain 2 spaces followed
by 2*n-5 stars and so on.. We can use
nested loops to print this pattern where
outer loop represents row number (say i)
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
and inner loop prints the space (i-1 times)
other conditions. Read our Privacy Policy
followed by the star pattern (2*n-(2*i-1)
times). Close and accept
https://www.techiedelight.com/c-program-print-pyramid-pattern/ 3/10
9/3/2019 Print pyramid pattern in C and Java - Techie Delight
1 #include <stdio.h>
2
3 int main(void)
4 {
5 // n is number of rows
6 int n = 5;
7
8 int i, j, k;
9
10 // do for each row
11 for (i = n; i >= 1; i--)
12 {
13 // print space
14 for (j = n; j > i; j--)
15 printf(" ");
16
17 // print '*'
18 for (k = 1; k < 2*i; k++)
19 printf("*");
20
21 // move to the next line
22 printf("\n");
23 }
24
25 return 0;
26 }
Download
Run Code
Java
1 class Pattern
2 {
3 public static void main(Strin
4 {
5 // n is number of rows
6 final int n = 5;
7
8 // Do for each row
9 for (int i = n; i >= 1; i
10 {
11 // Print space
12 for (int j = n; j > i
This website
13 uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
System.out.print(
other conditions.
14 Read our Privacy
} Policy
15
16 // Print '*'
Close and accept
17 for (int k = 1; k < 2
https://www.techiedelight.com/c-program-print-pyramid-pattern/ 4/10
9/3/2019 Print pyramid pattern in C and Java - Techie Delight
18 System.out.print(
19 }
20
21 // Move to the next l
22 System.out.print(Syst
23 }
24 }
25 }
Download
Run Code
Pattern 3: Hollow Pyramid
*
* *
* *
* *
* * * * * * * * *
The idea remains same as Pattern 1 but here
we print ‘*’ only for last row and rst & last
positions for remaining rows. For all other
positions, we print space.
1 #include <stdio.h>
2
3 int main(void)
4 {
5 // n is number of rows
6 int n = 5;
7
8 int i, j, k;
9
10 // do for each row
11 for (i = 1; i <= n; i++)
12 {
This website
13 uses cookies. By using
// print this site you agree to the use of cookies, our policies, copyright terms and
space
14 for (j = i; j < n; j++)
other conditions. Read our Privacy Policy
15 printf(" ");
16
17 // print '*' Close and accept
https://www.techiedelight.com/c-program-print-pyramid-pattern/ 5/10
9/3/2019 Print pyramid pattern in C and Java - Techie Delight
18 for (k = 1; k < 2*i; k++)
19 {
20 // print '*' for boun
21 if (i == n || (k == 1
22 printf("*");
23 else
24 printf(" ");
25 }
26
27 // move to the next line
28 printf("\n");
29 }
30
31 return 0;
32 }
Download
Run Code
Java
1 class Pattern
2 {
3 public static void main(Strin
4 {
5 // n is number of rows
6 final int n = 5;
7
8 // Do for each row
9 for (int i = 1; i <= n; i
10 {
11 // Print space
12 for (int j = i; j < n
13 System.out.print(
14 }
15
16 // Print '*'
17 for (int k = 1; k < 2
18 {
19 // Print '*' for
20 if (i == n || (k
21 System.out.pr
22 }
23 else {
24 System.out.pr
25 }
26 }
27
28 // Move to the next l
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
29 System.out.print(Syst
other conditions.
30 Read}our Privacy Policy
31 }
32 } Close and accept
https://www.techiedelight.com/c-program-print-pyramid-pattern/ 6/10
9/3/2019 Print pyramid pattern in C and Java - Techie Delight
Download
Run Code
Pattern 4: Hollow Inverted Pyramid
* * * * * * * * *
* *
* *
* *
*
The idea remains the same as Pattern 2 but
we print ‘*’ only for last row and rst & last
cell in each row. All other positions will be
lled by spaces.
1 #include <stdio.h>
2
3 int main(void)
4 {
5 // n is number of rows
6 int n = 5;
7
8 int i, j, k;
9
10 // do for each row
11 for (i = n; i >= 1; i--)
12 {
13 // print space
14 for (j = n; j > i; j--)
15 printf(" ");
16
17 // print '*'
18 for (k = 1; k < 2*i; k++)
19 {
20 // print '*' for boun
21 if (i == n || (k == 1
22 printf("*");
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
23 else
other conditions. Read our Privacy Policy
24 printf(" ");
25 }
26 Close and accept
https://www.techiedelight.com/c-program-print-pyramid-pattern/ 7/10
9/3/2019 Print pyramid pattern in C and Java - Techie Delight
27 // move to the next line
28 printf("\n");
29 }
30
31 return 0;
32 }
Download
Run Code
Java
1 class Pattern
2 {
3 public static void main(Strin
4 {
5 // n is number of rows
6 final int n = 5;
7
8 // Do for each row
9 for (int i = n; i >= 1; i
10 {
11 // Print space
12 for (int j = n; j > i
13 System.out.print(
14 }
15
16 // Print '*'
17 for (int k = 1; k < 2
18 // Print '*' for
19 if (i == n || (k
20 System.out.pr
21 }
22 else {
23 System.out.pr
24 }
25 }
26
27 // Move to the next l
28 System.out.print(Syst
29 }
30 }
31 }
Download
Run Code
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
other conditions. Read our Privacy Policy
(1 votes, average: 5.00 out of 5)
Close and accept
https://www.techiedelight.com/c-program-print-pyramid-pattern/ 8/10
9/3/2019 Print pyramid pattern in C and Java - Techie Delight
Thanks for reading.
Please use our online compiler to post code
in comments. To contribute, get in touch
with us.
Like us? Please spread the word and help us
grow. Happy coding 🙂
Sharing is caring:
Tweet Share 0 Share More
C, Java, Misc
Find maximum, Determine the if
minimum of three condition to print
numbers without speci c output
using conditional
statements
Leave a Reply
b i link b-quote u ul ol li
code spoiler
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
Start the discussion (Set your
other conditions. Read our Privacy Policy
avatar at
https://gravatar.com/)
Close and accept
https://www.techiedelight.com/c-program-print-pyramid-pattern/ 9/10
9/3/2019 Print pyramid pattern in C and Java - Techie Delight
Subscribe
Techie Delight © 2019 All Rights Reserved. Privacy Policy Contact us
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
other conditions. Read our Privacy Policy
Close and accept
https://www.techiedelight.com/c-program-print-pyramid-pattern/ 10/10