-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex13.c
54 lines (48 loc) · 1.2 KB
/
ex13.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Write a program that prints the following diamond shape. You may use printf statements that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with nested for statements) and minimize the number of printf statements
// *
// ***
// *****
// *******
// *********
// *******
// *****
// ***
// *
#include <stdio.h>
int main()
{
int i, j, rows = 5; // This will create a diamond with 9 rows in total.
// Print the upper half of the diamond
for (i = 1; i <= rows; i++)
{
// Print leading spaces
for (j = i; j < rows; j++)
{
printf(" ");
}
// Print asterisks
for (j = 1; j <= (2 * i - 1); j++)
{
printf("*");
}
// Move to the next line
printf("\n");
}
// Print the lower half of the diamond
for (i = 1; i <= rows - 1; i++)
{
// Print leading spaces
for (j = 1; j <= i; j++)
{
printf(" ");
}
// Print asterisks
for (j = 1; j <= (2 * (rows - i) - 1); j++)
{
printf("*");
}
// Move to the next line
printf("\n");
}
return 0;
}