Diamond pattern printing using stars

Write a program to print the diamond pattern.
C Program

#include <stdio.h>

int main()
{
    int n;

    printf("Enter number of rows: ");
    scanf("%d", &n);

    int spaceCount = n - 1;

    for (int i = 1i <= ni++)
    {

        //print number of spaces
        for (int j = 1j <= spaceCountj++)
        {
            printf(" ");
        }

        //print the *
        for (int j = 1j < 2 * ij++)
        {
            printf("*");
        }

        //print new line
        printf("\n");

        //decrement the count
        spaceCount--;
    }
    spaceCount = 1;

    for (int i = 1i <= n - 1i++)
    {

        //print spaces
        for (int j = 1j <= spaceCountj++)
        {
            printf(" ");
        }

        //print *
        for (int j = 1j < 2 * (n - i); j++)
        {
            printf("*");
        }

        printf("\n");
        spaceCount++;
    }

    return 0;
}
Output:

Enter number of rows: 5 * *** ***** ******* ********* ******* ***** *** *


No comments:

Post a Comment