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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c7.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c8.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c9.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c10.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c11.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c12.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c13.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c14.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c15.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c16.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c17.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c18.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c19.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c20.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c21.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c22.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c23.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c24.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c25.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c26.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c27.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c28.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c29.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c30.png)
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
![Star Program in C](https://static.javatpoint.com/cpages/images/star-program-in-c31.png) |
0 comments:
Post a Comment
If you have any doubts,Please let me know...