C Exercise 23
100 Classic C Language Examples
**Problem:** Print the following pattern (a diamond).
*
***
*****
*******
*****
***
*
**Analysis:** First treat the pattern as two parts: the first four rows follow one rule and the last three rows follow another. Use nested for loops, with the outer loop controlling rows and the inner loop controlling columns.
Program source code:
Example
// Created by www.runoob.com on 15/11/9.
// Copyright (c) 2015 Runoob Tutorial. All rights reserved.
//
#include
<
stdio.h
int
main
(
)
{
int
i
,
j
,
k;
for
(
i
=
0
;
i
<=
3
;
i
++
)
{
for
(
j
=
0
;
j
<=
2
i
;
j
++
)
{
printf
(
"
"
)
;
}
for
(
k
=
0
;
k
<=
2
i
;
k
++
)
{
printf
(
"
"
)
;
}
printf
(
"
\
n
"
)
;
}
for
(
i
=
0
;
i
<=
2
;
i
++
)
{
for
(
j
=
0
;
j
<=
i
;
j
++
)
{
printf
(
"
"
)
;
}
for
(
k
=
0
;
k
<=
4
2
i
;
k
++
)
{
printf
(
"
"
)
;
}
printf
(
"
\
n
"
)
;
}
}
The output of the above example is:
*
***
*****
*******
*****
***
*