Use the C language and use for loop to print the diamond (each adjacent diamond must contain spaces ).

Source: Internet
Author: User

Use the C language and use for loop to print the diamond (each adjacent diamond must contain spaces ).

I have seen some articles on the Internet that show how to print the diamond, but they are different from what I asked at the time. When the online code is output, each adjacent diamond does not contain spaces.

I am introducing the Code with spaces between each adjacent diamond.

The exercises and Code are as follows:

 

 

Use a solid diamond with * output side length of n.

*

**

***

**

*

 

 

Printf ("enter an integer :");

Int n = 0;

Scanf ("% d", & n );

 

// Print the number of rows. Note that the number of rows must be an odd number, so the number is 2n-1.

For (int I = 1; I <= 2 * n-1; I ++)

{

// Abs () is an absolute value function. The number of spaces in each line is = | n-number of rows |. Note that the number of spaces here refers to the number of spaces before !!

For (int j = 1; j <= abs (n-I); j ++)

{

Printf ("");

}

// "*" = N-| n-I | of each row. Note that, except for the first and last rows, multiple * Rows need to be printed, so you only need to add spaces after.

For (int k = 1; k <= n-abs (n-I); k ++)

{

Printf ("*");

}

Printf ("\ n ");

}

Related Article

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.