Outer loop loop control row, inner loop control column - print diamond

#include <stdio.h>
main()
{
    int i,j;
    for(i=0;i<4;i++)
    {
        for(j=0;j<=2-i;j++)
            printf(" ");
        for(j=0;j<=2*i;j++)
            printf("*");
        printf("\n");
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<=i;j++)
            printf(" ");
        for(j=0;j<=4-2*i;j++)
            printf("*");
        printf("\n");
    }
}

  Effect picture:

  *
  ***
 *****
*******
 *****
  ***
   *

Thought:

1. i for row and j for column.

2. The whole is divided into upper and lower parts. After the upper for loop is embedded, a newline character needs to be added to separate it, so as to continue to write the next for loop to represent the lower part.

3. In a single upper or lower part, it will be divided into " " and "*" parts, first express the space, and then express the *;

4. The relationship between the loops: It is recommended that the initial value is assigned to be 0, and then the relationship variable of the for loop is the functional relationship expression of i and j;

 

In general, this code took me half an afternoon. Although it was very brain-burning, I was really happy when I got it out~ I hope I can stick to it all the way.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325391785&siteId=291194637