Easy Tutorial
❮ C Exercise Example68 C Function Fmod ❯

C Exercise Example 23

C Language Classic 100 Examples

Title: Print the following pattern (diamond).

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

Program Analysis: First, consider the pattern as two parts: the first four lines follow one rule, and the last three lines follow another. Use a double for loop, the first layer controls the rows, and the second layer controls the columns.

Program Source Code:

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015年 tutorialpro.org. 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 outputs:

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

C Language Classic 100 Examples

❮ C Exercise Example68 C Function Fmod ❯