Star Pattern Print

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Star pattern

Program to print 8 star pattern


#include <stdio.h>
int main()
{
int i, j, size;
printf("Enter size: ");
scanf("%d", &size);
for(i=1; i<size*2; i++)
{
for(j=1; j<=size; j++)
{
// Condition for corner and center intersection space
if((i==1 && (j==1 || j==size)) ||
(i==size && (j==1 || j==size)) ||
(i==size*2-1 && (j==1 || j==size)))
{
printf(" ");
}
else if(i==1 || i==size || i==(size*2)-1 || j==1 || j==size)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Output
Enter size: 6
****
* *
* *
* *
* *
****
* *
* *
* *
* *
****
Program to print X star pattern
#include <stdio.h>
int main()
Star pattern

{
int i, j, N;
int count;
printf("Enter N: ");
scanf("%d", &N);
count = N * 2 - 1;
for(i=1; i<=count; i++)
{
for(j=1; j<=count; j++)
{
if(j==i || (j==count - i + 1))
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Before you move on. You might love to check the similar number pattern program

1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1

Output
Enter N: 5
* *
* *
* *
* *
*
* *
* *
* *
* *
Program to print heart star pattern
Star pattern

#include <stdio.h>
int main()
{
int i, j, n;
printf("Enter value of n : ");
scanf("%d", &n);
for(i=n/2; i<=n; i+=2)
{
for(j=1; j<n-i; j+=2)
{
printf(" ");
}
for(j=1; j<=i; j++)
{
printf("*");
}
for(j=1; j<=n-i; 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*2)-1; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter value of n: 10
***** *****
******* *******
********* *********
*******************
*****************
Star pattern

***************
*************
***********
*********
*******
*****
***
*
Program to print heart star pattern with name in center
#include <stdio.h>
#include <string.h>
int main()
{
int i, j, n;
char name[50];
int len;
printf("Enter your name: ");
gets(name);
printf("Enter value of n : ");
scanf("%d", &n);
len = strlen(name);
// Print upper part of the heart shape
for(i=n/2; i<=n; i+=2)
{
for(j=1; j<n-i; j+=2)
{
printf(" ");
}
for(j=1; j<=i; j++)
{
printf("*");
}
for(j=1; j<=n-i; j++)
{
printf(" ");
}
for(j=1; j<=i; j++)
{
printf("*");
}

printf("\n");
}
// Prints lower triangular part of the pattern
for(i=n; i>=1; i--)
{
for(j=i; j<n; j++)
Star pattern

{
printf(" ");
}
// Print the name
if(i == n)
{
for(j=1; j<=(n * 2-len)/2; j++)
{
printf("*");
}
printf("%s", name);

for(j=1; j<(n*2-len)/2; j++)


{
printf("*");
}
}
else
{
for(j=1; j<=(i*2)-1; j++)
{
printf("*");
}
}

printf("\n");
}

return 0;
}
Output
Enter your name: Codeforwin
Enter value of n: 10
***** *****
******* *******
********* *********
*****Codeforwin****
*****************
***************
*************
***********
*********
*******
*****
***
*
Program to print right arrow star pattern
Star pattern

#include <stdio.h>

int main()
{
int i, j, n;
// Input number of rows from user
printf("Enter value of n : ");
scanf("%d", &n);
// Print the upper part of the arrow
for(i=1; i<n; i++)
{
// Print trailing (2*rownumber-2) spaces
for(j=1; j<=(2*i-2); j++)
{
printf(" ");
}
// Print inverted right triangle star pattern
for(j=i; j<=n; j++)
{
printf("*");
}

printf("\n");
}
// Print lower part of the arrow
for(i=1; i<=n; i++)
{
// Print trailing (2*n - 2*rownumber) spaces
for(j=1; j<=(2*n - 2*i); j++)
{
printf(" ");
}
// Print simple right triangle star pattern
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter value of n : 5
*****
****
***
**
Star pattern

*
**
***
****
*****
Program to print half diamond star pattern
#include<stdio.h>
int main()
{
int i, j, N, columns;
/* Input number of columns from user */
printf("Enter number of columns:");
scanf("%d",&N);
columns=1;
for(i=1;i<N*2;i++)
{
for(j=1; j<=columns; j++)
{
printf("*");
}
if(i < N)
{
/* Increment number of columns per row for upper part */
columns++;
}
else
{
/* Decrement number of columns per row for lower part */
columns--;
}
/* Move to next line */
printf("\n");
}
return 0;
}
Output
Enter number of columns: 5
*
**
***
****
*****
****
***
**
*
Program to print mirrored half diamond star pattern
Star pattern

#include <stdio.h>
int main()
{
int i, j, N;
int star, spaces;
/* Input number of columns to print from user */
printf("Enter number of columns : ");
scanf("%d", &N);
spaces = N-1;
star = 1;
/* Iterate through rows */
for(i=1; i<N*2; i++)
{
/* Print leading spaces */
for(j=1; j<=spaces; j++)
printf(" ");
/* Print stars */
for(j=1; j<=star; j++)
printf("*");
/* Move to next line */
printf("\n");
if(i < N)
{
star++;
spaces--;
}
else
{
star--;
spaces++;
}
}
return 0;
}
Output
Enter number of columns : 5
*
**
***
****
*****
****
***
**
*
X Star Pattern
The code for the X star pattern is given below:
Star pattern

1. #include <stdio.h>
2. int main(void) {
3. int n,m;
4. printf("Enter the number");
5. scanf("%d",&n);
6. m=2*n-1;
7. for(int i=1;i<=m;i++)
8. {
9. for(int j=1;j<=m;j++)
10. {
11. if(i==j || j==(m-i+1))
12. {
13. printf("*");
14. }
15. else
16. {
17. printf(" ");
18. }
19. }
20. printf("\n");
21. }
22. return 0;
23. }
Output

Hollow Right Triangle Star Pattern


The code for the hollow right triangle star pattern is given below:
1. #include <stdio.h>
2. int main()
Star pattern

3. {
4. int n;
5. printf("Enter the number of rows");
6. scanf("%d",&n);
7. for(int i=1;i<=n;i++)
8. {
9. for(int j=1;j<=i;j++)
10. {
11. if(j==1|| i==j || i==n )
12. {
13. printf("*");
14. }
15. else
16. printf(" ");
17. }
18. printf("\n");
19. }
20. return 0;
21. }
Output

Rhombus Star Pattern


The code for the rhombus star pattern is given below:
1. #include <stdio.h>
2. int main()
3. {
4. int n;
Star pattern

5. printf("Enter the number of rows");


6. scanf("%d",&n);
7. for(int i=n;i>=1;i--)
8. {
9. for(int j=1;j<=i-1;j++)
10. {
11. printf(" ");
12. }
13. for(int k=1;k<=n;k++)
14. {
15. printf("*");
16. }
17. printf("\n");
18. }
19. return 0;
20. }
Output

Hollow Rhombus Star Pattern


The code for the hollow rhombus star pattern is given below:
1. #include <stdio.h>
2. int main()
3. {
4. int n;
5. printf("Enter the number of rows");
6. scanf("%d",&n);
7. for(int i=n;i>=1;i--)
8. {
Star pattern

9. for(int j=1;j<=i-1;j++)
10. {
11. printf(" ");
12. }
13. for(int k=1;k<=n;k++)
14. {
15. if(i==1 || i==n || k==1 || k==n)
16. printf("*");
17. else
18. printf(" ");
19. }
20. printf("\n");
21. }
22. return 0;
23. }
Output

Hollow Square Pattern with Diagonal


The code for the hollow square star pattern is given below:
1. #include <stdio.h>
2. int main()
3. {
4. int n;
5. printf("Enter the number of rows");
6. scanf("%d",&n);
7. for(int i=1;i<=n;i++)
8. {
9. for(int j=1;j<=n;j++)
Star pattern

10. {
11. if(i==1 ||i==n||j==1||j==n-i+1||i==j||j==n)
12. {
13. printf("*");
14. }
15. else
16. {
17. printf(" ");
18. }
19. }
20. printf("\n");
21. }
22. return 0;
23. }
Output

You might also like