Pattern Programs in C
Here in this article, we will cover the top example programs in C. Also, the rationale is all-inclusive and can be applied to various programming dialects, like C++, Java, and Python.
List of pattern programs in c:
1. Solid Rectangular Star Pattern in C
rows: 3
columns: 5
Preview
* * * * *
* * * * *
* * * * *
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
* * * * *
* *
* * * * *
Program
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
stars: 6
Preview
*
* *
* * *
* * * *
* * * * *
* * * * * *
Program
#include int main()
{
int stars,i, j;;printf("Enter the number of stars : ");
scanf("%d", &stars);/*Logic for half Pyramid*/
for (i = 1; i <= stars; i++){
for (j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}return 0;
}
Output
Enter the number of stars : 6
*
**
***
****
*****
******
4. Inverted Half Pyramid Pattern in C
stars: 6
Preview
* * * * * *
* * * * *
* * * *
* * *
* *
*
Program
int main()
{int stars,i, j;;printf("Enter the number of stars : ");scanf("%d", &stars);
/*Logic for inverted Pyramid*/
for (i = stars; i >= 1; i--){
for (j = 1 ; j <= i; j++)
{
printf("*");
}
printf("\n");
}return 0;
}
Output
Enter the number of stars : 6
******
*****
****
***
**
*
5. Full Pyramid Star Pattern in C
height=5
Preview
*
***
*****
*******
*********
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
*
* *
* *
* *
* * * * * * * * *
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
*
* *
* *
* *
* *
***********
Conclusion
Those were some of the most popular pattern programs in C.