Output a diamond consisting of a *

Source: Internet
Author: User
Tags printf

1. Output a diamond made of *

*

**

***

****

***

**

*

The idea of this problem is to think of this diamond as up and down two parts, belong to the use of circular statements written, looking for space and * between the law, such as the first line preceded by a space has a *, the second line to reduce a space plus a * so loop.

#include <stdio.h>
int main ()
{
int line = 0; Defines an integer variable line number. Suppose line is 7
int i = 0; Defining Shaping Variables I
scanf_s ("%d", &line); Number of input lines
for (i = 0; i < line; i++)//"Upper half of print" for loop,i< number of lines; i++
{
int j = 0; Define a shaping variable J
for (j = 0, J < line-i-1; j + +)//inline for loop "Print each line" when i=0 outputs first line, J Loop
{
printf (""); "Print Space" when i=0, j<6, output 6 spaces
}
for (j = 0; J < 2 * i + 1; j + +)//inline for loop
{
printf ("*"); "Print *" I equals 0, output A *, loop
}
printf ("\ n");
}//"Print bottom half"
for (i = 0; i < line-1; i++)
{
int j = 0;
for (j = 0; J < i + 1; j + +)
{
printf ("");
}
for (j = 0; J < 2 * (Line-1-i)-1; j + +)
{
printf ("*");
}
printf ("\ n");
}
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.