Monday, 13 June 2016

Star Diamond


Firstly we will take a for loop(outer) for no of rows (n).

For the 1st (n/2 + 1) rows

Print spaces
For every ith row print (n/2) - i spaces.

Print stars
For every ith row print i + 1 stars.


For the last n/2 rows

Print spaces
For every ith row print i spaces.

Print stars
For every ith row print (n/2) - i + 1 stars.

#include<stdio.h>

int main()
{
    int i,j,k,n;

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

    for(i=0;i<=n/2;i++)
    {
        for(j=i;j<n/2;j++)
            printf(" ");

        for(j=0;j<=i;j++)
            printf("* ");

        printf("\n");
    }

    for(i=1;i<=n/2;i++)
    {
        for(j=1;j<=i;j++)
            printf(" ");

        for(j=i;j<=n/2;j++)
            printf("* ");

        printf("\n");
    }

    return 0;
}

No comments:

Post a Comment