Professional Documents
Culture Documents
*
*
*
*
*
*
*
*
*
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--) {
09
printf(" ");
10
11
12
// Spacing factor
printf("%2c", prnt);
13
14
15
printf("\n");
--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() {
03 char prnt = '*';
04 int i, j, k, s, c = 1, nos = 9;
05 for (i = 1; c <= 4; i++) {
06
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
18
if (c == 4 && k == 5) {
19
break;
20
21
printf("%2c", prnt);
22
23
printf("\n");
24
25
nos = nos - 4;
++c;
26
27 }
28 return 0;
29 }
Download Code
Back to top
*
*
*
*
*
*
*
*
*
*
*
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
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--) {
19
printf("
");
20
21
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;
34 }
nos = 1;
// space control
// remaining half..
printf("
");
}
}
for (s = nos; s >= 1; s--) {
48
printf("
");
49
50
51
if ((k % 2) != 0) {
52
printf("%3c", prnt);
53
54 else {
55
printf("
56
");
57
58
59
nos = nos + 2;
printf("\n");
60 }
61 return 0;
// space control
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
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
07
printf("
");
08
09
10
11
if ((i % 2) != 0 && (j % 2) != 0) {
printf("%2c", prnt);
12
} else {
13
printf("
");
14
15
16
17
printf("\n");
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
25
26
27
if ((i % 2) != 0 && (j % 2) != 0) {
printf("%2c", prnt);
28
} else {
29
printf("
30
");
31
32
33
nos--;
printf("\n");
34 }
35 return 0;
36 }
Download Code
Back to top
*
* *
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* *
* * *
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
10
11
printf("%2c", prnt);
}
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
25
26
27
28
printf("%2c", prnt);
29
30
31
nos++;
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
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 /*
02
This can be seen as two right angle triangles sharing the same
base
03
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--) {
12
printf("
// Spacing factor
");
13 }
14 for (j = 1; j <= i; j++) {
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);
25 nos++;
26 printf("\n");
27 nos = 7;
int j = 1;
30
31
triangle(nos, i);
nos = nos - j;
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
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
31
32
break;
33
34
printf("%2c", prnt);
35
36
37
nos = nos - 2;
printf("\n");
38 }
39 return 0;
40 }
Download Code
Back to top
* * * *
* * *
* *
*
* * * *
* * * *
* * *
* *
*
*
*
*
*
* * * *
* * *
* *
*
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
07
printf("
");
08
09
10 printf("%2c", prnt);
11 }
12 for (sp = nosp; sp >= 1; sp--) {
13
printf(" ");
14
15
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
31
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 /*
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 *
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 *
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
37
triangle(nosp, i, 1);
triangle(nbsp, i, 1);
38
printf("\n");
39
nos++;
40
41
nosp = nosp + 2;
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
47
triangle(nosp, i, 1);
triangle(nbsp, i, 1);
48
printf("\n");
49
nos--;
50
51
nosp = nosp - 2;
nbsp = nbsp - 2;
52 }
53 return 0;
54 }