C Exercise Example 23

100 Classic C Examples

Title:Print the following pattern (diamond).

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

Program analysis:First, consider the figure as two parts: the first four rows follow one pattern, and the last three rows follow another. Use a double for loop, with the outer loop controlling rows and the inner loop controlling columns.

Program source code:

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. 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 output of the above example is:

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

100 Classic C Examples

other extensions