Saturday 11 June 2016

Star triangle



Let n = number of rows

Firstly we will take a for loop(outer) for 3 rows.
In the outer for loop,we need 2 for loops(one for spaces and other for stars).

Print spaces
For every ith row , print n-i spaces.
Row 1 3 - 1 = 2
Row 2 3 - 2 = 1
Row 3 3 - 3 = 0

Print stars
For every ith row , print 2*i-1 stars.
Row 1 2*1-1 = 1
Row 2 2*2-1 = 3
Row 3 2*3-1 = 5
#include<stdio.h>

int main()
{
    int i,j;

    for(i=1;i<=3;i++)
    {
        for(j=i;j<3;j++)
            printf(" ");

        for(j=1;j<=2*i-1;j++)
            printf("*");

        printf("\n");
    }

    return 0;
}

No comments:

Post a Comment