Print diamond on screen with c


#include < stdio.h >

/*
printing diamond shape
For a given number of rows 
the odd number of columns needs to be calculated

rows
mzx_columns = (rows*2) + 1

print in increasing order
iterator1 0 to rows
    print spaces
        iterator2 (rows - iterator1) to 0
    print the char
        iterator3 0 to (iterator1 * 2) + 1
    print new line

print in decreasing order
iterator1 (rows - 2) to 0
    print spaces
        iterator2 (rows - iterator1) to 0
    print char
        iterator3 0 to (iterator1 * 2) + 1
    print new line

*/

int main()
{
    int rows;

    printf("Enter the number of rows to print: ");
    scanf("%d", &rows);

    for(int i=0; i < rows; i++)
    {
        for(int j=rows-i; j >= 0; j--) printf(" ");
        for(int j=0; j < (i*2)+1; j++) printf("*");
        printf("\n");
    }

    for(int i=rows-2; i >= 0; i--)
    {
        for(int j=rows-i; j >= 0; j--) printf(" ");
        for(int j=0; j < (i*2)+1; j++) printf("*");
        printf("\n");
    }

}


Output:

Comments

Popular Posts