Print a diamond code of a specified size under a C + + command prompt form
Under VS2010, create a new empty project. Add the source file and paste the code in it.
The size of the diamond can be controlled by changing the size of the MaxRows value.
#include <stdio.h> #include <cstdlib>//Add dependent header file void Main () {int I,j,k;int maxRows = 12;//controls the size of the diamond// First, print the top four lines for (i=1;i<maxrows;i++) {for (j=1;j<maxrows-i;j++)//control the number of spaces to print printf (""); for (k=1;k<=2*i-1;k++)// Controls the number of asterisks to print printf ("*");p rintf ("\ n");} Print the bottom three lines for (i=1;i<=maxrows-2;i++) {for (j=1;j<=i;j++)//control the number of spaces to print printf (""); for (k=1;k<= (maxrows-i) * 2-3; k++)//control the number of asterisks to print printf ("*");p rintf ("\ n");} System ("pause");//For display please press the random key to continue ... so that the program will not quit a bit. }
:
Print a diamond code of a specified size under a C + + command prompt form