100% found this document useful (3 votes)
8K views16 pages

10 Challenging Star Pattern Programs in C

The document provides code snippets for 10 challenging star pattern programs in C. Each program prints a unique star pattern and includes the C code to generate that pattern using loops and conditional statements. Key aspects like spacing factors and nested loops are used to control the placement of stars and spaces to build the various patterns.

Uploaded by

Vinay Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
8K views16 pages

10 Challenging Star Pattern Programs in C

The document provides code snippets for 10 challenging star pattern programs in C. Each program prints a unique star pattern and includes the C code to generate that pattern using loops and conditional statements. Key aspects like spacing factors and nested loops are used to control the placement of stars and spaces to build the various patterns.

Uploaded by

Vinay Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

10 Challenging star pattern programs in C

1. Write a C program to print the following pattern:

*
* *
* * *
* * * *

Program:

view source
print?
01 /* This is a simple mirror-image of a right angle triangle */
02  
03 #include <stdio.h>
04 int main() {

05  char prnt = '*';


06  int i, j, nos = 4, s;
07  for (i = 1; i <= 5; i++) {
08 for (s = nos; s >= 1; s--) {  // Spacing factor
09    printf("  ");
10   }
11   for (j = 1; j <= i; j++) {
12    printf("%2c", prnt);

13   }
14   printf("\n");
15   --nos;   // Controls the spacing factor
16  }
17  return 0;
18 }

Download Code

Back to top

2. Write C program to print the following pattern:


3. * *
4. * * * * * *
5. * * * * * * * * * *
* * * * * * * * * * *

Program:
view source
print?
01 #include<stdio.h>
02 int main() {

03  char prnt = '*';


04  int i, j, k, s, c = 1, nos = 9;
05  for (i = 1; c <= 4; i++) {
     // As we want to print the columns in odd sequence viz.
06
1,3,5,.etc
07   if ((i % 2) != 0) {
08    for (j = 1; j <= i; j++) {
09  printf("%2c", prnt);
10 }
11 for (s = nos; s >= 1; s--) { //The spacing factor
12     if (c == 4 && s == 1) {
13      break;
14     }
15     printf("  ");
16    }
17    for (k = 1; k <= i; k++) {
18     if (c == 4 && k == 5) {
19      break;
20     }
21     printf("%2c", prnt);
22    }

23    printf("\n");
24    nos = nos - 4;  // controls the spacing factor
25    ++c;
26   }

27  }
28  return 0;
29 }

Download Code

Back to top

6. Write C program to print the following pattern:


7. * *
8. * *
9. * * * *
10. * * * *
11. * * * * *
12. * * * *
13. * * * *
14. * *
* *

Program:

view source
print?
01 #include<stdio.h>
02 int main() {

03  char prnt = '*';


04  int i, j, k, s, p, r, nos = 7;
05  
06  for (i = 1; i <= 5; i++) {
07   for (j = 1; j <= i; j++) {
08  if ((i % 2) != 0 && (j % 2) != 0) {
09 printf("%3c", prnt);
10 }
11 else if ((i % 2) == 0 && (j % 2) == 0) {
12 printf("%3c", prnt);

13 }
14 else {
15 printf("   ");
16 }

17 }
18 for (s = nos; s >= 1; s--) {  // for the spacing factor
19    printf("   ");
20   }
21   for (k = 1; k <= i; k++) { //Joining seperate figures
22 if (i == 5 && k == 1) {
23  continue;
24 }

25 if ((k % 2) != 0) {
26 printf("%3c", prnt);
27 }
28 else {
29 printf("   ");
30 }

31 }
32 printf("\n");
33 nos = nos - 2;   // space control
34 }  nos = 1;  // remaining half..

35 for (p = 4; p >= 1; p--) {


36   for (r = 1; r <= p; r++) {
37 if ((p % 2) != 0 && (r % 2) != 0) {
38 printf("%3c", prnt);

39 }
40 else if ((p % 2) == 0 && (r % 2) == 0) {
41 printf("%3c", prnt);
42 }

43 else {
44 printf("   ");
45 }
46 }
47 for (s = nos; s >= 1; s--) {
48    printf("   ");

49   }
50   for (k = 1; k <= p; k++) {
51    if ((k % 2) != 0) {
52     printf("%3c", prnt);
53    }
54 else {
55     printf("   ");
56    }

57   }
58   nos = nos + 2;  // space control
59   printf("\n");
60  }
61  return 0;
62 }
Download Code

Explanation:
This can be seen as an inverted diamond composed of stars. It can be noted that the
composition of this figure follows sequential pattern of consecutive stars and spaces.

In case of odd row number, the odd column positions will be filled up with ‘*’, else a
space will be spaced and vice-versa in case of even numbered row.

In order to achieve this we will construct four different right angle triangles

aligned as per the requirement.


Back to top

15. Write a C program to print the following pattern:


16. * * * * *
17. * * * *
18. * * *
19. * *
20. *
21. * *
22. * * *
23. * * * *
* * * * *

Program:

view source
print?
01 #include<stdio.h>
02 int main() {

03  char prnt = '*';


04  int i, j, s, nos = 0;
05  for (i = 9; i >= 1; (i = i - 2)) {
06   for (s = nos; s >= 1; s--) {
07    printf("  ");
08   }

09   for (j = 1; j <= i; j++) {


10    if ((i % 2) != 0 && (j % 2) != 0) {
11     printf("%2c", prnt);
12    } else {
13     printf("  ");
14    }
15   }
16   printf("\n");
17   nos++;
18  }

19  nos = 3;
20  for (i = 3; i <= 9; (i = i + 2)) {
21 for (s = nos; s >= 1; s--) {
22    printf("  ");

23   }
24   for (j = 1; j <= i; j++) {
25  
26    if ((i % 2) != 0 && (j % 2) != 0) {
27     printf("%2c", prnt);
28    } else {
29     printf("  ");
30    }

31   }
32   nos--;
33   printf("\n");
34  }
35  return 0;
36 }

Download Code
Back to top

24. Write a C program to print the following pattern:


25. *
26. * * *
27. ** * * *
28. * ** * * * *
29. * * ** * * * * *
30. * ** * * * *
31. ** * * *
32. * * *
33. *
34. * * *
* * * * *

Program:

view source
print?
01 #include<stdio.h>
02 int main() {

03  char prnt = '*';


04  int i, j, k, s, nos = 4;
05  for (i = 1; i <= 5; i++) {
06 for (s = nos; s >= 1; s--) {
07    printf("  ");
08   }
09   for (j = 1; j <= i; j++) {
10    printf("%2c", prnt);

11   }
12   for (k = 1; k <= (i - 1); k++) {
13 if (i == 1) {     continue;
14 }
15 printf("%2c", prnt);
16 }
17  printf("\n");   nos--;
18 }

19  nos = 1;
20 for (i = 4; i >= 1; i--) {
21   for (s = nos; s >= 1; s--) {
22    printf("  ");

23   }
24   for (j = 1; j <= i; j++) {
25    printf("%2c", prnt);
26   }
27   for (k = 1; k <= (i - 1); k++) {
28    printf("%2c", prnt);

29   }
30   nos++;
31   printf("\n");
32  }

33  nos = 3;
34  for (i = 2; i <= 5; i++) {
35 if ((i % 2) != 0) {
36 for (s = nos; s >= 1; s--) {
37     printf("  ");
38    }
39    for (j = 1; j <= i; j++) {
40     printf("%2c", prnt);
41    }
42   }
43   if ((i % 2) != 0) {
44    printf("\n");
45    nos--;
46   }

47  }
48  return 0;
49 }

Download Code
Back to top

35. Write a C program to print the following pattern:


36. *
37. * *
38. * * *
39. * * * *
40. * * *
41. * *
*

Program:

view source
print?
01 /*
    This can be seen as two right angle triangles sharing the same
02
base
03     which is modified by adding few extra shifting spaces
04 */

05 #include <stdio.h>
06 // This function controls the inner loop and the spacing
07 // factor guided by the outer loop index and the spacing index.
08 int triangle(int nos, int i) {
09  char prnt = '*';
10  int s, j;
11  for (s = nos; s >= 1; s--) {    // Spacing factor
12   printf("  ");

13  }
14  for (j = 1; j <= i; j++) {      //The inner loop
15   printf("%2c", prnt);
16  }
17  return 0;
18 }

19  
20 int main() {
21  int i, nos = 5;
22  //draws the upper triangle
23  for (i = 1; i <= 4; i++) {
24  triangle(nos, i);    //Inner loop construction
25  nos++;              // Increments the spacing factor
26  printf("\n");  }
27 nos = 7;  //Draws the lower triangle skipping its base.
28 for (i = 3; i >= 1; i--) {

29   int j = 1;
30   triangle(nos, i);  // Inner loop construction
31   nos = nos - j;     // Spacing factor
32   printf("\n");

33  }
34  return 0;
35 }

Download Code
Back to top

42. Write a C program to print the following pattern:


43. * * * * * * * * *
44. * * * * * * * *
45. * * * * * *
46. * * * *
47. * *
48. * * * *
49. * * * * * *
50. * * * * * * * *
* * * * * * * * *

Program:
view source
print?
01 #include <stdio.h>
02  
03 int main() {
04  char prnt = '*';
05  int i, j, k, s, nos = -1;
06  for (i = 5; i >= 1; i--) {
07   for (j = 1; j <= i; j++) {
08  printf("%2c", prnt);

09 }
10 for (s = nos; s >= 1; s--) {
11    printf("  ");
12   }
13   for (k = 1; k <= i; k++) {
14    if (i == 5 && k == 5) {
15     continue;
16    }
17    printf("%2c", prnt);
18   }
19   nos = nos + 2;
20   printf("\n");

21  }
22  nos = 5;
23  for (i = 2; i <= 5; i++) {
24   for (j = 1; j <= i; j++) {
25  printf("%2c", prnt);
26 }
27 for (s = nos; s >= 1; s--) {
28    printf("  ");

29   }
30   for (k = 1; k <= i; k++) {
31    if (i == 5 && k == 5) {
32     break;

33    }
34    printf("%2c", prnt);
35   }
36   nos = nos - 2;
37   printf("\n");
38  }
39  return 0;
40 }

Download Code
Back to top

51. Write a C program to print the following pattern:


52. * * * * * * * * * * * * * * * * *
53. * * * * * * * * * * * * * *
54. * * * * * * * * * *
55. * * * * * *
56. * * * * * * * * *
57. * * * * * * *
58. * * * * *
59. * * *
*

Program:

view source
print?
01 #include <stdio.h>
02 int main() {

03  char prnt = '*';


04  int i, j, k, s, sp, nos = 0, nosp = -1;
05  for (i = 9; i >= 3; (i = i - 2)) {
06   for (s = nos; s >= 1; s--) {
07    printf("  ");
08   }
09   for (j = 1; j <= i; j++) {
10 printf("%2c", prnt);

11 }
12 for (sp = nosp; sp >= 1; sp--) {
13    printf("  ");
14   }
15   for (k = 1; k <= i; k++) {
16  if (i == 9 && k == 1) {
17 continue;
18 }
19 printf("%2c", prnt);
20 }

21 nos++;
22 nosp = nosp + 2;
23 printf("\n");
24 }

25 nos = 4;
26 for (i = 9; i >= 1; (i = i - 2)) {
27   for (s = nos; s >= 1; s--) {
28    printf("  ");

29   }
30   for (j = 1; j <= i; j++) {
31    printf("%2c", prnt);
32   }

33   nos++;
34   printf("\n");
35  }
36  
37  return 0;
38 }

Download Code
Back to top

60. Write a C program to print the following pattern:


61. *
62. * * *
63. * * * * *
64. * * * * * * *
65. * *
66. * * * *
67. * * * * * *
68. * * * * * * *
69. * * * * * *
70. * * * *
71. * *
72. * * * * * * *
73. * * * * *
74. * * *
*

Program:
view source
print?
01 #include <stdio.h>
02 /*

03  * nos = Num. of spaces required in the triangle.


04  * i   = Counter for the num. of charcters to print in each row
05  * skip= A flag for checking whether to
06  *       skip a character in a row.

07  *
08  */
09 int triangle(int nos, int i, int skip) {
10  char prnt = '*';

11  int s, j;
12  for (s = nos; s >= 1; s--) {
13   printf("  ");
14  }
15  for (j = 1; j <= i; j++) {
16   if (skip != 0) {
17    if (i == 4 && j == 1) {
18     continue;
19    }
20   }
21   printf("%2c", prnt);
22  }
23  return 0;
24 }

25  
26 int main() {
27  int i, nos = 4;
28  for (i = 1; i <= 7; (i = i + 2)) {
29   triangle(nos, i, 0);
30   nos--;
31   printf("\n");
32  }

33  nos = 5;
34  for (i = 1; i <= 4; i++) {
35 triangle(1, i, 0); //one space needed in each case of the formation
36 triangle(nos, i, 1); //skip printing one star in the last row.
37 nos = nos - 2;
38 printf("\n");

39 }
40 nos = 1;
41 for (i = 3; i >= 1; i--) {
42   triangle(1, i, 0);
43   triangle(nos, i, 0);
44   nos = nos + 2;
45   printf("\n");
46  }

47  nos = 1;
48  for (i = 7; i >= 1; (i = i - 2)) {
49   triangle(nos, i, 0);
50   nos++;
51   printf("\n");
52  }
53  return 0;
54 }

Download Code
Back to top

75. Write a C program to print the following pattern:


76. * * * * * * * * * * * * * * * * * * * * * * * * *
77. * * * * * *
78. * * * * * *
79. * * * * * *
80. * * *
81. * * * * * *
82. * * * * * *
83. * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * *

Program:

view source
print?
01 #include <stdio.h>
02  
03 /*
04  * nos = Num. of spaces required in the triangle.
05  * i   = Counter for the num. of characters to print in each row
06  * skip= A flag for check whether to
07  *       skip a character in a row.
08  *
09  */
10  
11 int triangle(int nos, int i, int skip) {
12  char prnt = '*';

13  int s, j;
14  for (s = nos; s >= 1; s--) {
15   printf("  ");
16  }
17  for (j = 1; j <= i; j++) {
18  if (skip != 0) {
19  if (i == 9 && j == 1) {
20    continue;
21  }
22  }
23 if (i == 1 || i == 9) {
24  printf("%2c", prnt);

25 }
26 else if (j == 1 || j == i) {
27  printf("%2c", prnt);
28  } else {
29  printf("  ");
30 }  }

31 return 0; }
32 int main() {
33 int i, nos = 0, nosp = -1, nbsp = -1;
34 for (i = 9; i >= 1; (i = i - 2)) {

35   triangle(nos, i, 0);
36   triangle(nosp, i, 1);
37   triangle(nbsp, i, 1);
38   printf("\n");
39   nos++;
40   nosp = nosp + 2;
41   nbsp = nbsp + 2;
42  }

43  nos = 3, nosp = 5, nbsp = 5;


44  for (i = 3; i <= 9; (i = i + 2)) {
45   triangle(nos, i, 0);
46   triangle(nosp, i, 1);
47   triangle(nbsp, i, 1);
48   printf("\n");

49   nos--;
50   nosp = nosp - 2;
51   nbsp = nbsp - 2;
52  }
53  return 0;
54 }

You might also like