Star Patterns Program in C
In this topic, we will learn how to create the patterns by using C language. We will create the patterns by using either a '*' star character or some other character. We will create different patterns or a geometrical shape such as triangle, square, etc.
We are going to cover the following patterns. You can view the code of any pattern given below by clicking on the pattern.
Square Star Pattern
The code to create the square star pattern is given below:
- #include <stdio.h>
- int main()
- {
- int n;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<n;j++)
- {
- printf("*");
- }
- printf("\n");
- }
-
- return 0;
- }
Output

Hollow Square Star Pattern
Now, we create the hollow square star pattern. The source code for this pattern is given below:
- int main()
- {
- int n;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=n;j++)
- {
- if(i==1 ||i==n||j==1||j==n)
- {
- printf("*");
- }
- else
- printf(" ");
- }
- printf("\n");
- }
-
- return 0;
- }
Output

Hollow Square Pattern with Diagonal
The code for the hollow square star pattern is given below:
- #include <stdio.h>
- int main()
- {
- int n;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=n;j++)
- {
- if(i==1 ||i==n||j==1||j==n-i+1||i==j||j==n)
- {
- printf("*");
- }
- else
- {
-
- printf(" ");
- }
-
- }
- printf("\n");
- }
-
- return 0;
- }
Output

Rhombus Star Pattern
The code for the rhombus star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=n;i>=1;i--)
- {
- for(int j=1;j<=i-1;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=n;k++)
- {
- printf("*");
- }
- printf("\n");
- }
- return 0;
- }
Output

Hollow Rhombus Star Pattern
The code for the hollow rhombus star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=n;i>=1;i--)
- {
- for(int j=1;j<=i-1;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=n;k++)
- {
- if(i==1 || i==n || k==1 || k==n)
- printf("*");
- else
- printf(" ");
- }
- printf("\n");
- }
- return 0;
- }
Output

Mirrored Rhombus Star Pattern
The code for the mirrored rhombus star pattern is given below:
- #include <stdio.h>
- int main()
- {
- int n;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<i;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=n;k++)
- {
- printf("*");
-
- }
- printf("\n");
- }
- return 0;
- }
Output

Hollow Mirrored Rhombus Star Pattern
The code for the hollow mirrored rhombus star pattern is given below:
- #include <stdio.h>
- int main()
- {
- int n;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<i;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=n;k++)
- {
- if(i==1 || i==n || k==1 || k==n)
- printf("*");
- else
- printf(" ");
- }
- printf("\n");
- }
- return 0;
- }
Output

Right Triangle Star Pattern
The code for the right triangle star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=i;j++)
- {
- printf("* ");
- }
- printf("\n");
- }
- return 0;
- }
Output

Hollow Right Triangle Star Pattern
The code for the hollow right triangle star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=i;j++)
- {
- if(j==1|| i==j || i==n )
- {
- printf("*");
- }
- else
- printf(" ");
- }
- printf("\n");
- }
- return 0;
- }
Output

Mirrored Right Triangle Star Pattern
The code for the mirrored right triangle star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n,m=1;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=n;i>=1;i--)
- {
- for(int j=1;j<=i-1;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=m;k++)
- {
- printf("*");
- }
- printf("\n");
- m++;
- }
- return 0;
- }
Output

Hollow Mirrored Right Triangle Star Pattern
The code for the hollow mirrored right triangle star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n,m=1;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=n;i>=1;i--)
- {
- for(int j=1;j<=i-1;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=m;k++)
- {
- if(k==1 || k==m || m==n)
- printf("*");
- else
- printf(" ");
- }
- printf("\n");
- m++;
- }
- return 0;
- }
Output

Inverted Right Triangle Star Pattern
The code for the inverted right triangle star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n,m=1;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=n;i>=1;i--)
- {
- for(int j=1;j<=i;j++)
- {
- printf("*");
- }
- printf("\n");
- }
- return 0;
- }
Output

Hollow Inverted Right Triangle Star Pattern
The code for the hollow inverted right triangle star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n,m=1;
- printf("Enter the number of rows");
- scanf("%d",&n);
- for(int i=n;i>=1;i--)
- {
- for(int j=1;j<=i;j++)
- {
- if(j==1 || j==i || i==n)
- printf("*");
- else
- printf(" ");
- }
- printf("\n");
- }
- return 0;
- }
Output

Inverted Mirrored Right Triangle Star Pattern
The code for the inverted mirrored right triangle star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n,m;
- printf("Enter the number of rows");
- scanf("%d",&n);
- m=n;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<i;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=m;k++)
- {
- printf("*");
- }
- m--;
-
- printf("\n");
- }
- return 0;
- }
Output

Hollow Inverted Mirrored Right Triangle Star Pattern
The code for the hollow inverted mirrored right triangle star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n,m;
- printf("Enter the number of rows");
- scanf("%d",&n);
- m=n;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<i;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=m;k++)
- {
- if(i==1 || k==1 || k==m)
- printf("*");
- else
- printf(" ");
- }
- m--;
-
- printf("\n");
- }
- return 0;
- }
Output

Pyramid Star Pattern
The code for the pyramid star pattern is given below:
- #include <stdio.h>
- int main()
- {
- int n,m;
- printf("Enter the number of rows");
- scanf("%d",&n);
- m=n;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=m-1;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=2*i-1;k++)
- {
- printf("*");
- }
- m--;
-
- printf("\n");
- }
- return 0;
- }
Output

Hollow Pyramid Star Pattern
The code for the hollow pyramid star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n,m;
- printf("Enter the number of rows");
- scanf("%d",&n);
- m=n;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=m-1;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=2*i-1;k++)
- {
- if(k==1 || k==2*i-1 || i==n)
- printf("*");
- else
- printf(" ");
- }
- m--;
-
- printf("\n");
- }
- return 0;
- }
Output

Inverted Pyramid Star Pattern
The code for the inverted pyramid is given below:
- #include <stdio.h>
-
- int main()
- {
- int n,m=1;
- printf("Enter the number of rows");
- scanf("%d",&n);
-
- for(int i=n;i>=1;i--)
- {
- for(int j=1;j<m;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=2*i-1;k++)
- {
- printf("*");
- }
- m++;
-
- printf("\n");
- }
- return 0;
- }
Output

Hollow Pyramid Star Pattern
The code for the hollow pyramid star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n,m=1;
- printf("Enter the number of rows");
- scanf("%d",&n);
-
- for(int i=n;i>=1;i--)
- {
- for(int j=1;j<m;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=2*i-1;k++)
- {
- if(k==1 || k==2*i-1 || i==n)
- printf("*");
- else
- printf(" ");
- }
- m++;
-
- printf("\n");
- }
- return 0;
- }
Output

Half Diamond Star Pattern
The code for the half diamond star pattern is given below:
- #include <stdio.h>
-
- int main()
- {
- int n,m=1;
- printf("Enter the number of columns");
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=i;j++)
- {
- printf("*");
- }
- printf("\n");
- }
- for(int i=n-1;i>=1;i--)
- {
- for(int j=1;j<=i;j++)
- {
- printf("*");
- }
- printf("\n");
- }
-
- return 0;
- }
Output

Diamond Star Pattern
The code for the diamond star pattern is given below:
- #include <stdio.h>
- int main(void) {
- int n;
- printf("Enter the number of rows\n");
- scanf("%d",&n);
- int spaces=n-1;
- int stars=1;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=spaces;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=stars;k++)
- {
- printf("*");
- }
- if(spaces>i)
- {
- spaces=spaces-1;
- stars=stars+2;
- }
- if(spaces<i)
- {
- spaces=spaces+1;
- stars=stars-2;
- }
- printf("\n");
- }
- return 0;}
Output

Right Arrow Star Pattern
The code of the right arrow star pattern is given below:
- #include <stdio.h>
-
- int main(void) {
-
- int n;
- printf("Enter the number of columns");
- scanf("%d",&n);
-
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<i;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=n-i;k++)
- {
- printf("*");
- }
- printf("\n");
- }
- for(int i=1;i<n;i++)
- {
- for(int j=1;j<n-i;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=i+1;k++)
- {
- printf("*");
- }
- printf("\n");
- }
- return 0;
- }
Output

Left Arrow Star Pattern
The code for the Left arrow star pattern is given below:
- #include <stdio.h>
-
- int main(void) {
-
- int n;
- printf("Enter the number of columns");
- scanf("%d",&n);
-
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=n-i;j++)
- {
- printf(" ");
- }
- for(int k=0;k<=n-i;k++)
- {
- printf("*");
- }
- printf("\n");
- }
- for(int i=1;i<n;i++)
- {
- for(int j=1;j<i+1;j++)
- {
- printf(" ");
- }
- for(int k=1;k<=i+1;k++)
- {
- printf("*");
- }
- printf("\n");
- }
- return 0;
- }
Output

Plus Star Pattern
The code for the plus star pattern is given below:
- #include <stdio.h>
-
- int main(void) {
- int n;
- printf("Enter the odd number only");
- scanf("%d", &n);
- for(int i=1;i<=n;i++)
- {
- if(i==((n/2)+1))
- {
- for(int j=1;j<=n;j++)
- {
- printf("+");
- }
-
- }
- else
- {
- for(int j=1;j<=n/2;j++)
- {
- printf(" ");
- }
- printf("+");
- }
- printf("\n");
- }
- return 0;
- }
Output

X Star Pattern
The code for the X star pattern is given below:
- #include <stdio.h>
-
- int main(void) {
- int n,m;
- printf("Enter the number");
- scanf("%d",&n);
- m=2*n-1;
- for(int i=1;i<=m;i++)
- {
- for(int j=1;j<=m;j++)
- {
- if(i==j || j==(m-i+1))
- {
- printf("*");
- }
- else
- {
- printf(" ");
- }
- }
- printf("\n");
- }
- return 0;
- }
Output
 |
0 comments:
Post a Comment
If you have any doubts,Please let me know...