Updated May 19, 2023
Introduction of Star pattern in C
In this article, first, we will see how to use C programming to work with various star pattern programs with the help of examples. In any programming language, star patterns are a common pattern widely used because it helps to improve logical thinking and flow control knowledge. You must use two or three loops to create a star pattern in the C language. The number of loops depends on the pattern that you need to create. For pattern, a minimum of two is used i.e. one for a row and one for a column. The First loop is called an outer loop that shows the rows, and the second loop is called an inner loop that shows columns.
Examples of Star Patterns in C language
Let us discuss some examples to understand the concept in C language easily.
Example 1: Program in C to print a star pyramid pattern.
In the following program, the user can enter the number of rows to print the star pyramid pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, i, j;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
//for loop for displaying space
for(s = i; s < n; s++)
{
printf(" ");
}
//for loop to display star equal to row number
for(j = 1; j <= (2 * i - 1); j++)
{
printf("*");
}
// ending line after each row
printf("\n");
}
}
Output:
Example 2: Program in C to enter the number of rows.
In the following program, the user can enter the number of rows to print the star pyramid pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, i, j;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i = n; i >= 1; i--)
{
//for loop to put space
for(s = i; s < n; s++)
printf(" ");
//for loop for displaying star
for(j = 1; j <= (2 * i - 1); j++)
printf("* ");
// ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 3: Program in C to print a half-star pyramid pattern.
In the following program, the user can enter the number of rows to print the half-star pyramid pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int i, j, n;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
printf("* ");
}
//Ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 4: Program in C to print the inverted half-star pyramid pattern.
In the following program, the user can enter the number of rows to print the inverted half-star pyramid pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int i, j, n;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i = n; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
printf("* ");
}
// ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 5: Program to print the full star diamond pattern in C.
In the following program, the user can enter the number of rows for the diamond dimension to print the diamond pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, i, j;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i = 0; i <= n; i++)
{
for(s = n; s > i; s--)
printf(" ");
for(j=0; j<i; j++)
printf("* ");
printf("\n");
}
for(i = 1; i < n; i++)
{
for(s = 0; s < i; s++)
printf(" ");
for(j = n; j > i; j--)
printf("* ");
// ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 6: Program in C to print an inverted star pyramid pattern.
In the following program, the user can enter the number of rows to print the inverted star pyramid pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, i, j;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i = n; i >= 1; i--)
{
//for loop to put space
for(s = i; s < n; s++)
printf(" ");
//for loop for displaying star
for(j = 1; j <= i; j++)
printf("* ");
// ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 7: Program in C to enter the number of rows.
In the following program, the user can enter the number of rows to print the star pyramid pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, i, j;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
//for loop to put space
for(s = i; s < n; s++)
printf(" ");
//for loop for displaying star
for(j = 1; j <= i; j++)
printf("* ");
// ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 8: Program in C to enter the number of rows to print.
In the following program, the user can enter the number of rows to print the star pyramid pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i , j;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
for(i = n; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
printf( "*");
}
// ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 9: Program in C to enter the number of rows to print the star
In the following program, the user can enter the number of rows to print the star pyramid pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = i; j < n; j++)
{
printf(" ");
}
for(j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
for(i = n; i >= 1; i--)
{
for(j = i; j <= n; j++)
{
printf(" ");
}
for(j = 1; j < i; j++)
{
printf("*");
}
// ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 10: C+ Program to print the hollow star pyramid in a diamond pattern.
In the following C program, the user can enter the number of rows to print the hollow star pyramid in a diamond pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Enter size of Daimond: ");
int n, i, j, m = 1, k;
scanf("%d",&n);
for(i = 0; i <= n; i++)
{
for(j = n; j > i; j--)
{
printf(" ");
}
printf("*");
if (i > 0)
{
for(k = 1; k <= m; k++)
{
printf(" ");
}
m += 2;
printf("*");
}
printf("\n");
}
m -= 4;
for(i = 0; i <= n-1; i++)
{
for(j = 0; j <= i; j++)
{
printf(" ");
}
printf("*");
for(k = 1; k <= m; k++)
{
printf(" ");
}
m -= 2;
if(i != n-1)
{
printf ("*");
}
//ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 11: Program to print hollow star pyramid pattern in C.
In the following program, the user can enter the number of rows to print the hollow star pyramid pattern as he wishes, then the result will be displayed on the screen:
#include<stdio.h>
#include<conio.h>
int main()
{
int r, i, j, s;
printf("Enter number of rows: ");
scanf("%d",&r);
for(i = 1; i <= r; i++)
{
//for loop to put space in pyramid
for (s = i; s < r; s++)
printf(" ");
//for loop to print star
for(j = 1; j <= (2 * r - 1); j++)
{
if(i == r || j == 1 || j == 2*i - 1)
printf("*");
else
printf(" ");
}
//ending line after each row
printf("\n");
}
return 0;
}
Output:
Example 12: Program to print an inverted hollow star pyramid pattern in C.
In the following program, the user can enter the number of rows to print the inverted hollow star pyramid pattern as he wishes, then the result will be displayed on the screen:
C program to display the inverted hollow star pyramid.
#include<stdio.h>
#include<conio.h>
int main()
{
int r, i, j, s;
printf("Enter number of rows: ");
scanf("%d",&r);
for(i = r; i >= 1; i--)
{
//for loop to put space in pyramid
for (s = i; s < r; s++)
printf(" ");
//for loop to print star in pyramid
for(j = 1; j <= 2 * i - 1; j++)
{
if(i == r || j == 1 || j == 2*i - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output:
Conclusion
So here we have seen programs for how to print various Star patterns. How to print the Star pyramid pattern and the inverted hollow star pyramid pattern in the number of rows. Includes Formulas to grow better and understand step by step.
Recommended Articles
This has been a guide to Star Patterns in C. Here we discuss the examples of star patterns in C language with 12 different images and codes. You can also go through our other suggested articles to learn more-