C language outputs a diamond pattern on the screen
Output case on the screen: **************************************** **************************************** * ***** observe the variation pattern of each row, find the algorithm. The Code is as follows:
# Include <stdio. h> int main () {int I, j, k; for (I = 0; I <7; I ++)/* I is a row, first output the first 7 rows */{for (j = 0; j <6-i; j ++)/* output * leading space */{printf ("");} for (k = 6-i; k <I + 7; k ++)/* output * (observe the variation of each line) */{printf ("*");} printf ("\ n") ;}for (I = 7; I <13; I ++)/* output the last 6 rows */{for (j = 0; j <i-6; j ++) {printf ("") ;}for (k = i-7; k <18-i; k ++) {printf ("*");} printf ("\ n");} return 0 ;}
This method cannot dynamically output a diamond image, but only the image. The code after improvement is as follows:
#include<stdio.h>int main(){ int line,i,j,k; scanf("%d",&line); for(i=0;i<line;i++) { for(j=0;j<line-i-1;j++) { printf(" "); } for(k=0;k<2*i+1;k++) { printf("*"); } printf("\n"); } for(i=line;i<2*line;i++) { for(j=0;j<i+1-line;j++) { printf(" "); } for(k=0;k<2*line-2*(i-(line-1))-1;k++) { printf("*"); } printf("\n"); } return 0;}