Star Program in C
Star Program in C
*
* *
* * *
* * * *
Program:
02
03 #include <stdio.h>
04 int main() {
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 }
D ow n l oa d C od e
B ac k t o t op
Program:
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++) {
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");
26 }
27 }
28 return 0;
29 }
D ow n l oa d C od e
B ac k t o t op
Program:
01 #include<stdio.h>
02 int main() {
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
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 }
60 }
61 return 0;
62 }
D ow n l oa d C od e
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:
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 }
D ow n l oa d C od e
B ac k t o t op
Program:
01 #include<stdio.h>
02 int main() {
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 }
D ow n l oa d C od e
B ac k t o t op
Program:
01 /*
02 This can be seen as two right angle triangles sharing the same base
03 which is modified by adding few extra shifting spaces
04 */
05 #include <stdio.h>
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;
23 for (i = 1; i <= 4; i++) {
26 printf("\n"); }
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 }
D ow n l oa d C od e
B ac k t o t op
Program:
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 }
D ow n l oa d C od e
B ac k t o t op
Program:
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 }
D ow n l oa d C od e
B ac k t o t op
Program:
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
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 }
D ow n l oa d C od e
B ac k t o t op
Program:
view source
print?
01 #include <stdio.h>
02
03 /*
08 *
09 */
10
11 int triangle(int nos, int i, int skip) {
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 }
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 }
D ow n l oa d C od e
B ac k t o t op