Latest web development tutorials

C Exercise Example 23

100 cases of classic C language 100 cases of classic C language

Topic: Print out the following pattern (diamond).

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

Program analysis: first to look at the graphic divided into two parts, the first four lines of a law, a law after three lines, the use of a double for the cycle, the first layer of the control line, the second layer of the control column.

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. 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 above example output is:

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

100 cases of classic C language 100 cases of classic C language