Use the C language to print the diamond with A For loop (with spaces for each adjacent diamond)

Source: Internet
Author: User

Before I saw some articles on the internet about how to print the diamond, but not the same as I asked at that time, the online code output when each adjacent diamond has no space.

What I'm going to introduce is a code with spaces between each of the adjacent diamonds.

The exercises and code are as follows:

A solid diamond with a * output edge length of N.

*

* *

* * *

* *

*

printf ("Please enter an integer:");

int n = 0;

scanf ("%d", &n);

Print the number of lines, note that it must be an odd line, so it is 2n-1

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

{

ABS () is the absolute value function, the number of spaces per line =|n-lines |. Note that the number of spaces here refers to the number of spaces before the * number!!

for (int j = 1; J <= Abs (N-i); j + +)

{

printf ("");

}

Each line of "*" =n-|n-i|. Note Because the remaining rows require more than one * to be printed, except for the first and last lines, you only need to add a space after the *.

for (int k = 1; k <= N-abs (n-i); k++)

{

printf ("*");

}

printf ("\ n");

}

Use the C language to print the diamond with A For loop (with spaces for each adjacent diamond)

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.