If you are a novice learning C programming, you will definitely come across pattern programs.
Pattern programs are a type of programming exercise where you write a program to print several patterns, such as a square, diamond, pyramid, and other shapes, using a symbol, number, or alphabet.
To write these programs, you must have a good grasp of the following C concepts:
- if...else statement
- for loop
- while loop
- do...while loop
- break and continue statements
Practicing these programs will help you gain hands-on experience with loops, nested loops, and conditional statements. Besides, you will significantly improve your programming skills by building program logic.
Pattern programs are commonly asked during interviews. Moreover, the logic is universal and can be applied to different programming languages , such as C++, Java, Python, and JavaScript.
In this article, we will cover the top pattern programs in C. We will use the '*' symbol as a pixel or unit to write a program; hence, they are called Star Pattern Programs .
Pattern Programs in C
The patterns that we will be discussing are:
- Solid Rectangular Star Pattern in C
- Hollow Rectangular star Pattern in C
- Half Pyramid Star Pattern in C
- Inverted Half Pyramid Pattern in C
- Full Pyramid Star Pattern in C
- Hollow Pyramid Star Pattern
- Inverted Hollow Half pyramid in C
- Full Hollow Inverted Pyramid in C
- Half Diamond Star pattern in C
- Solid Full Diamond Pattern in C
- Right Arrow Star Pattern in C
- Left Arrow Star Pattern
- Plus Star Pattern
- X Pattern in C
- The Diagonal Hollow Square Pattern in C
- Solid Rhombus Star Pattern in C
- Hollow Rhombus Star Pattern in C
- Solid Mirrored Rhombus Pattern in C
- Hollow Mirrored Rhombus Star Pattern
- Pascal's triangle in C
- Floyd's Triangle in C
- Half Pyramid of Alphabets in C
- Palindrome Half Pyramid Pattern in Alphabets
- Palindrome Half Pyramid Pattern Using Numbers in C
- Hour Glass Star Pattern program in C
1. Solid Rectangular Star Pattern in C
Rows: 3
Columns: 5
Preview
*****
*****
*****
Logic
The logic for this pattern program is straightforward. In this star design, we only need a nested loop.
In that nested loop, the outer loop (i) will act as a row, the inner loop (j) will act as a column, and the star printing statement will reside in the inner loop.
- The outer loop (i) will start from 0 to rows (number of rows).
- The inner loop (i) will also start from 0 up to columns (number of columns)
Pseudocode
FOR i = 1; i <= rows; i++:
FOR j = 1; j <= columns; j++:
PRINT(*)
PRINT("\n") #new line
ENDFOR
Program
#include <stdio.h>
int main()
{
int rows, columns,i,j;
printf("Enter the number of rows : ");
scanf("%d", &rows);
printf("Enter the number of columns : ");
scanf("%d", &columns);
printf("\n");
/* print solid rectangle*/
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= columns; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows : 3
Enter the number of columns : 5
*****
*****
*****
2. Hollow Rectangular Star Pattern in C
Rows: 3
Columns: 5
Preview
* * * * *
* *
* * * * *
Logic
In the hollow rectangular design pattern, we also require a single nested
for
loop.
For the hollow effect in the pattern, we need to put some conditions inside the inner loop (j), which decides whether to print a * or white space .
In the inner loop (j), we will only print the * pattern when
i==1 || i==rows || j==1 || j==columns
Pseudocode
FOR i = 1; i <= rows; i++:
for j = 1; j <= columns; j++:
IF i==1 || i==rows || j==1 || j==columns:
PRINT("*") #PRINT STAR
ELSE:
PRINT(" ") #PRINT WHITE SPACE
PRINT("\n") #NEW LINE
Program
#include <stdio.h>
int main()
{
int rows, columns,i, j;
printf("Enter the number of rows : ");
scanf("%d", &rows);
printf("Enter the number of columns : ");
scanf("%d", &columns);
printf("\n");
/*Logic for holow rectangle*/
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= columns; j++)
{
if (i==1 || i==rows || j==1 || j==columns)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows : 4
Enter the number of columns : 5
*****
* *
* *
*****
3. Half Pyramid Star Pattern in C
Height: 6
Preview
*
**
***
****
*****
******
Logic
The logic for printing the pyramid and triangle can be more tricky than printing square or rectangular patterns in C. This is because, in the pyramid or triangle patterns, we do not accept rows and columns; instead, we look for the total height of the pyramid. The inner loop (j) statement's starting or ending points depend on the outer loop (i) value .
In the half-pyramid pattern, we also require a nested loop where
- The outer loop (i) will start from 1 to the total number of starts or the pyramid's height.
- The inner loop(j) will also start from 1 but ends at the current value of the outer loop (i)
Pseudocode
FOR i = 1; i <= height; i++:
FOR j = 1; j <= i; j++:
PRINT("*")
PRINT("\n") # NEW LINE
Program
#include <stdio.h>
int main()
{
int height,i, j;;
printf("Enter the height of the pyramid : ");
scanf("%d", &height); //height of the pyramid
/*Logic for half Pyramid*/
for (i = 1; i <= height; i++)
{
for (j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter the height of the pyramid : 6
*
**
***
****
*****
******
4. Inverted Half Pyramid Pattern in C
Height: 6
Preview
******
*****
****
***
**
*
Logic
The logic for this pattern program in C is similar to the above program. The only difference is, here, instead of using an increment for the loop, we will use a decrement outer for loop statement.
Pseudocode
FOR i = height; i >= 1; i--:
FOR j = 1 ; j <= i; j++:
PRINT("*")
PRINT("\n")
Program
#include <stdio.h>
int main()
{
int height,i, j;;
printf("Enter the height of the pyramid : ");
scanf("%d", &height); //height of the pyramid
/*Logic for inverted Pyramid*/
for (i = height; i >= 1; i--)
{
for (j = 1 ; j <= i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter the height of the pyramid : 6
******
*****
****
***
**
*
5. Full Pyramid Star Pattern in C
Height = 5
Preview
*
***
*****
*******
*********
Logic
In this pattern design, we need to print an equilateral triangle. We, again, need a nested loop.
But there is a catch in this pattern. The nested loop contains two inner
for
loops
j
and
k
, where
j
will be responsible for printing the white space, and
k
will print the star for the pattern.
Pseudocode
space = height;
FOR i=1;i<=height;i++:
FOR j=1;j<space-1;j++:
PRINT(" ")
FOR k=1;k<=2*i-1;k++:
PRINT("*")
space -=1
PRINT("\n") #NEW LINE
Program
#include <stdio.h>
int main()
{
int height,space,i,j,k;
printf("Enter the height of the triangle: ");
scanf("%d",&height);
space=height;
for( i=1;i<=height;i++)
{
for( j=1;j<space-1;j++)
{
printf(" ");
}
for( k=1;k<=2*i-1;k++)
{
printf("*");
}
space--;
printf("\n");
}
return 0;
}
Output
Enter the height of the triangle: 5
*
***
*****
*******
*********
6. Hollow Pyramid Star Pattern
Height of Pyramid: 6
Preview
*
* *
* *
* *
* *
***********
Logic
We can use the following logic to print the hollow pyramid star pattern in C.
- Initialize a variable space equal to the height of the pyramid
- Run the outer loop i from 1 to the height of the triangle.
- In the inner loop, j runs the loop from 1 to space-1 for printing the space.
-
Again, in the inner loop,
k
runs the loop from
1 to 2*i-1
. In the
k
loop, check for the condition
k==1 OR k==2*i-1 OR i==height
print *; else, print a space. - After the k loop decrement, the value of space by 1.
Pseudocode:
space = height
FOR i=1;i<=height;i++:
FOR j=1;j<=space-1;j++:
PRINT(" ")
FOR k=1;k<=2*i-1;k++:
IF(k==1 || k==2*i-1 || i==height):
PRINT("*")
ELSE:
PRINT(" ")
space -= 1;
Program
#include<stdio.h>
int main()
{
int height,space,i,j,k;
printf("Height of Pyramid: ");
scanf("%d",&height);
space=height;
for( i=1;i<=height;i++)
{
for( j=1;j<=space-1;j++)
{
printf(" ");
}
for( k=1;k<=2*i-1;k++)
{
if(k==1 || k==2*i-1 || i==height)
printf("*");
else
printf(" ");
}
space--;
printf("\n");
}
return 0;
}
Output
Height of Pyramid: 6
*
* *
* *
* *
* *
***********
7. Inverted Hollow Half Pyramid in C
Height = 7
Preview
*******
* *
* *
* *
* *
**
*
Logic
We can follow the following logic to print the inverted design pattern in the C programming language .
- Run the outer for loop i from total height to 1.
- Run the inner loop j from 1 to the current value of i
-
In the inner loop, check for the condition
j==1 || j==i || i==height
and print *; else, print the white space.
Pseudocode
FOR i=height;i>=1;i--:
FOR j=1;j<=i;j++:
IF(j==1 OR j==i OR i==height):
PRINT("*")
ELSE:
PRINT(" ")
Program
#include<stdio.h>
int main()
{
int height,i,j;
printf("Enter the height of Pyramid: ");
scanf("%d",&height);
for(i=height;i>=1;i--)
{
for( j=1;j<=i;j++)
{
if(j==1 || j==i || i==height)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output
Enter the height of Pyramid: 7
*******
* *
* *
* *
* *
**
*
8. Full Hollow Inverted Pyramid in C
Height: 6
Preview
***********
* *
* *
* *
* *
*
Logic
Logic to print the full hollow inverted pyramid:
- Set the value of space = 1.
- Run the outer for loop in decrement order from height to 1.
- Run the inner first loop j from i to space, and print the space.
-
Run the second inner loop k from 1 to 2*i-1. Check for the condition
k==1 || k==2*i-1 || i==height
that prints the star; otherwise, print the white space. - Increase the value of space by 1.
Pseudocode
space = height
FOR i=height;i>=1;i--:
FOR j=1;j<space;j++:
PRINT(" ")
FOR k=1;k<=2*i-1;k++:
IF k==1 OR k==2*i-1 OR i==height:
PRINT("*")
ELSE:
PRINT(" ")
SPACE +=1
PRINT("\n")
Program
#include <stdio.h>
int main()
{
int height,space=1,i,j,k;
printf("Enter the height of pyramid: ");
scanf("%d",&height);
for( i=height;i>=1;i--)
{
for( j=1;j<space;j++)
{
printf(" ");
}
for( k=1;k<=2*i-1;k++)
{
if(k==1 || k==2*i-1 || i==height)
printf("*");
else
printf(" ");
}
space++;
printf("\n");
}
return 0;
}
Output
Enter the height of pyramid: 6
***********
* *
* *
* *
* *
*
9. Half Diamond Star Pattern in C
Max Width: 7
Preview
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
Logic
This pattern design is a combination of half pyramid and half inverted pyramid.
To design the upper half "half pyramid," we will use the nested loop, in which the outer loop
i
will go 1 to the max width of the pattern, and the inner loop
j
will print the
i
number of stars in each row.
To design the lower half "half inverted pyramid", the outer loop will start from max width - 1 and ends with 1, and the inner loop j will print the i numbers of stars in each row.
Pseudocode
//upper half
FOR i=1; i<= max_width; i++:
FOR j=1; j<=i; j++:
PRINT("*")
PRINT("\n") //print new line
//lower half
FOR i=max_stars-1 ;i>=1 ;i--:
FOR j=1; j<=i; j++:
PRINT("*")
PRINT("\n") //print new line
Program
#include<stdio.h>
int main()
{
int max_width,i,j;
printf("Enter the width of diamond: ");
scanf("%d",&max_width);
//upper half of the daimond
for( i=1;i<=max_width;i++)
{
for( j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
//lowerhalf of the daimond
for( i=max_width-1;i>=1;i--){
for( j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter the width of diamond: 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
10. Solid Full Diamond Pattern in C
Width: 7
Preview
*
***
*****
*******
*****
***
*
Logic
This pattern can be designed using either two nested loops or a single nested loop.
We can design this pattern in C with the combination of the full pyramid and the full inverted pyramid, but it will take two nested loops.
We also have an alternative approach - using a single nested loop with some conditions and printing this design.
To design this pattern, the only thing we need to keep in mind is how many stars and spaces we want to print in each row.
Pseudocode
space = width-1
stars = 1
FOR i=1; i<=width; i++:
FOR j=1; j<=space; j++:
PRINT(" ") //print space numbers of white space " "
FOR k=1; k<=stars; k++:
PRINT("*") // print stars number of *
IF space>i:
space=space-1;
stars=stars+2;
IF space<i:
space=space+1;
stars=stars-2;
PRINT("\n") //NEW LINE
Program
#include<stdio.h>
int main(void) {
int width, space, stars, i, j, k;
printf("Enter the width of diamonds: ");
scanf("%d",&width);
space=width-1;
stars=1;
for( i=1;i<=width;i++)
{
for( j=1;j<=space;j++)
{
printf(" ");
}
for( k=1;k<=stars;k++) { printf("*"); } if(space>i)
{
space=space-1;
stars=stars+2;
}
if(space<i)
{
space=space+1;
stars=stars-2;
}
printf("\n");
}
return 0;}
Output
Enter the width of diamonds: 7
*
***
*****
*******
*****
***
*
11. Right Arrow Star Pattern in C
Width: 7
Preview
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
Logic
In this design pattern, the space before the stars can be designed using inverted half and half pyramids.
For the star pattern, we can print the width-i number of stars for the upper half and i+1 numbers of stars for the lower part.
Pseudocode
//upper part
FOR i=0; i<width; i++:
FOR j=0; j<i; j++:
PRINT(" ")
FOR k=1;k<=width-i;k++:
PRINT("*")
PRINT("\n") //new line
//lower part
FOR i=1; i<width; i++:
FOR j=1; j<width-i; j++:
PRINT(" ")
FOR k=1; k<=i+1; k++:
PRINT("*")
PRINT("\n")
Program
#include<stdio.h>
int main(void) {
int width,i,j,k;
printf("Enter the width of arrow: ");
scanf("%d",&width);
// arrow upper part
for( i=0;i<width;i++)
{
for( j=0;j<i;j++)
{
printf(" ");
}
for( k=1;k<=width-i;k++)
{
printf("*");
}
printf("\n");
}
// arrow lower part
for( i=1;i<width;i++)
{
for( j=1;j<width-i;j++)
{
printf(" ");
}
for( k=1;k<=i+1;k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter the width of arrow: 7
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
12. Left Arrow Star Pattern in C
Width: 6
Preview
******
*****
****
***
**
*
**
***
****
*****
******
Logic
We can divide the complete program into two parts to design the left arrow star pattern in C.
To print the upper and lower part of the arrow, we will use the two nested double loops.
In the nested loop, the outer loop
i
will go from 1 up to the arrow's width. Whereas, the inner loops
j
and
k
will be printing the spaces and stars for the pattern.
Pseudocode
//upper half of the arrow
FOR i=1;i<=width;i++:
//loop to print the space
FOR j=1;j<=width-i;j++:
PRINT(" ");
//loop to print the stars
FOR k=0;k<=width-i;k++:
PRINT("*");
PRINT("\n");
//lower half of the arrow
FOR i=1;i<width;i++:
// loop to print the space
FOR j=1;j<i+1;j++:
printf(" ");
// loop to print the stars
FOR k=1;k<=i+1;k++:
PRINT("*");
PRINT("\n");
Program
#include<stdio.h>
int main(void) {
int width,i,j,k;
printf("Enter the width of arrow: ");
scanf("%d",&width);
for( i=1;i<=width;i++)
{
for( j=1;j<=width-i;j++)
{
printf(" ");
}
for( k=0;k<=width-i;k++)
{
printf("*");
}
printf("\n");
}
for( i=1;i<width;i++)
{
for( j=1;j<i+1;j++)
{
printf(" ");
}
for( k=1;k<=i+1;k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter the width of arrow: 6
******
*****
****
***
**
*
**
***
****
*****
******
13. Plus Star Pattern in C
Height: 7
Preview
+
+
+
+++++++
+
+
+
Program
#include<stdio.h>
int main() {
int height,i,j;
printf("Enter the height (odd): ");
scanf("%d", &height);
for( i=1;i<=height;i++)
{
if(i==((height/2)+1))
{
for( j=1;j<=height;j++)
{
printf("+");
}
}
else
{
for( j=1;j<=height/2;j++)
{
printf(" ");
}
printf("+");
}
printf("\n");
}
return 0;
}
Output
Enter the height (odd): 7
+
+
+
+++++++
+
+
+
14. X Pattern in C
Width: 6
Preview
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
Program
#include<stdio.h>
int main() {
int width,space, i,j;
printf("Enter the width: ");
scanf("%d",&width);
space=2*width-1;
for( i=1;i<=space;i++)
{
for( j=1;j<=space;j++)
{
if(i==j || j==(space-i+1))
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Output
Enter the width: 6
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
15. The Diagonal Hollow Square Pattern in C
Rows: 8
Preview
********
** **
* * * *
* ** *
* ** *
* * * *
** **
********
Program
#include<stdio.h>
int main()
{
int rows, i, j;
printf("Enter rows: ");
scanf("%d",&rows);
for( i=1;i<=rows;i++)
{
for( j=1;j<=rows;j++)
{
if(i==1 ||i==rows||j==1||j==rows-i+1||i==j||j==rows)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Output
Enter rows: 8
********
** **
* * * *
* ** *
* ** *
* * * *
** **
********
16. Solid Rhombus Star Pattern in C
Rows: 7
Preview
*******
*******
*******
*******
*******
*******
*******
Program
#include<stdio.h>
int main()
{
int rows, i,j,k;
printf("Enter rows: ");
scanf("%d",&rows);
for( i=rows;i>=1;i--)
{
for( j=1;j<=i-1;j++)
{
printf(" ");
}
for( k=1;k<=rows;k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter rows: 7
*******
*******
*******
*******
*******
*******
*******
17. Hollow Rhombus Star Pattern in C
Rows: 7
Preview
*******
* *
* *
* *
* *
* *
*******
Program
#include<stdio.h>
int main()
{
int rows,i,j,k;
printf("Enter rows: ");
scanf("%d",&rows);
for( i=rows;i>=1;i--)
{
for( j=1;j<=i-1;j++)
{
printf(" ");
}
for( k=1;k<=rows;k++)
{
if(i==1 || i==rows || k==1 || k==rows)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output
Enter rows: 7
*******
* *
* *
* *
* *
* *
*******
18. Solid Mirrored Rhombus Pattern in C
Rows: 7
Preview
*******
*******
*******
*******
*******
*******
*******
Program
#include<stdio.h>
int main()
{
int rows,i,j,k;
printf("Enter rows: ");
scanf("%d",&rows);
for( i=1;i<=rows;i++)
{
for( j=1;j<i;j++)
{
printf(" ");
}
for( k=1;k<=rows;k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter rows: 7
*******
*******
*******
*******
*******
*******
*******
19. Hollow Mirrored Rhombus Star Pattern
Rows: 7
Preview
*******
* *
* *
* *
* *
* *
*******
Program
#include <stdio.h>
int main()
{
int rows, i,j,k;
printf("Enter rows: ");
scanf("%d",&rows);
for( i=1;i<=rows;i++)
{
for( j=1;j<i;j++)
{
printf(" ");
}
for( k=1;k<=rows;k++)
{
if(i==1 || i==rows || k==1 || k==rows)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output
Enter rows: 7
*******
* *
* *
* *
* *
* *
*******
20. Pascal's Triangle in C
Rows: 6
Preview
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
6 7 8 9 10 11 10 9 8 7 6
Program
#include <stdio.h>
int main() {
int i, s, r, k = 0, c1 = 0, c2 = 0;
printf("Enter rows(r): ");
scanf("%d", &r);
for (i = 1; i <= r; ++i) {
for (s = 1; s <= r - i; ++s) {
printf(" ");
++c1;
}
while (k != 2 * i - 1) {
if (c1 <= r - 1) {
printf("%d ", i + k);
++c1;
} else {
++c2;
printf("%d ", (i + k - 2 * c2));
}
++k;
}
c2 = c1 = k = 0;
printf("\n");
}
return 0;
}
Output
Enter rows(r): 6
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
6 7 8 9 10 11 10 9 8 7 6
21. Floyd's Triangle in C
Rows: 6
Preview
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Program
#include<stdio.h>
int main() {
int rows, i, j, n = 1;
printf("Enter rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", n);
++n;
}
printf("\n");
}
return 0;
}
Output
Enter rows: 6
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22. Half Pyramid of Alphabets in C
Last Alphabet: F
Preview
A
B B
C C C
D D D D
E E E E E
F F F F F F
Program
#include<stdio.h>
int main() {
int i, j;
char input, alphabet = 'A';
printf("Enter an uppercase character you want to print in the last row: ");
scanf("%c", &input);
for (i = 1; i <= (input - 'A' + 1); ++i) {
for (j = 1; j <= i; ++j) {
printf("%c ", alphabet);
}
++alphabet;
printf("\n");
}
return 0;
}
Output
Enter an uppercase character you want to print in the last row: F
A
B B
C C C
D D D D
E E E E E
F F F F F F
23. Palindrome Half Pyramid Patterns in Alphabets
Rows: 5
Preview
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
Program
#include<stdio.h>
int main() {
int i, j, rows, c=0;
printf("Enter rows: ");
scanf("%d", &rows);
for (i = 1; i <= 2*rows; i=i+2) {
for (j = 1; j <= i; j++) {
printf("%c", 'A'+c);
if(j <= i/2)
c++;
else
c--;
}
c = 0;
printf("\n");
}
return(0);
}
Output
Enter rows: 6
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
Note: Check out the C program to print a triangle of alphabets .
24. Palindrome Half Pyramid Pattern Using Numbers in C
Rows: 6
Preview
1
121
12321
1234321
123454321
12345654321
Program
#include <stdio.h>
int main() {
int i, j,k, rows, c=0;
printf("Enter rows: ");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
for(j=1;j<=i;j++) { printf("%d",j); } for(k=i-1;k>=1;k--)
{
printf("%d",k);
}
printf("\n");
}
return(0);
}
Output
Enter rows: 6
1
121
12321
1234321
123454321
12345654321
25. Hour Glass Star Pattern in C
Width = 7
Preview
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Logic
This star pyramid pattern combines a full pyramid and an inverted full pyramid star pattern. We can divide this star pattern into two sub-star patterns: upper and lower Half.
The upper half pattern will be a full inverted pyramid.
And the lower half pattern will be the full pyramid.
Pseudocode
n = INPUT("width of the hour glass");
//upper half Pyramid
FOR i 1 -> n:
FOR k 1 -> i:
PRINT(" ");
FOR j i->n;
PRINT("*");
PRINT(" ");
PRINT("\n");
//lower half Pyramid
FOR i n->1; i--:
FOR k 1->1; k++:
PRINT(" ");
FOR j i ->n; j++:
PRINT("*");
PRINT(" ");
PRINT("\n");
Program
#include<stdio.h>
int main()
{
int n, i,j,k;
printf("Enter the width of the HourGlass: ");
scanf("%d",&n);
// for loop for printing
// upper half of the Hour Glass
for (i = 1; i <= n; i++) {
// printing i spaces at
// the beginning of each row
for (k = 1; k < i; k++)
printf(" ");
// printing i to rows value
// at the end of each row
for (j = i; j <= n; j++){
printf("*");
//space after every star print
printf(" ");
}
//new line for each row
printf("\n");
}
// for loop for printing
//the lower half of the Hour Glass
for (i = n - 1; i >= 1; i--)
{
// printing i spaces at the
// beginning of each row
for (k = 1; k < i; k++)
printf(" ");
// printing i to width number of * in each rows
for (j = i; j <= n; j++){
printf("*");
//space after every start print
printf(" ");
}
printf("\n");
}
return 0;
}
Output
Enter the width of the HourGlass: 7
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Conclusion
These were some of the most popular pattern programs in C. You can try many other patterns with different numbers and alphabets. These programs will improve your understanding of C loops and conditional statements. Also, you will have a better understanding of developing program logic.
What are your favorite pattern programs? Let us know in the comments.
People are also reading:
Leave a Comment on this Post