Professional Documents
Culture Documents
10 Challenging Star Pattern Programs in C
10 Challenging Star Pattern Programs in C
*
* *
* * *
* * * *
Program:
view source
print?
01 /* This is a simple mirror-image of a right angle triangle */
02
03 #include <stdio.h>
04 int main() {
13 }
14 printf("\n");
15 --nos; // Controls the spacing factor
16 }
17 return 0;
18 }
Download Code
Back to top
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
23 printf("\n");
24 nos = nos - 4; // controls the spacing factor
25 ++c;
26 }
27 }
28 return 0;
29 }
Download Code
Back to top
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
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..
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
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
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
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
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
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
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
Program:
view source
print?
01 #include <stdio.h>
02 int main() {
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
Program:
view source
print?
01 #include <stdio.h>
02 /*
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
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 }
49 nos--;
50 nosp = nosp - 2;
51 nbsp = nbsp - 2;
52 }
53 return 0;
54 }