Thursday, 2 January 2014

Pattern

A program for displaying pattern.



int main()
{
    int i, j, s, h,next,nos = 0;

 printf("Enter height: ");
scanf("%d",&h);

 //nos controls the spacing.
//1st triangle

for (i = 7; i >= 1; (i= i - 2)) {
for (s = nos; s >= 1; s--) {  //Spacing control
printf(" ");
}
for (j = 0; j <= i; j++) {
if (j == 1 || j == i) { //This hollows the triangle
printf("*");
} else {
printf(" ");
}
}
        printf("\n");


nos++;  // In the upper triangle the space increments by 1.
}
/* Since both the triangles have the same peak point, while printing the second triangle we'll ignore its peak point. Thus i starts from 3 and the nos from 3.*/

nos = 3;
for (i = 3; i <= 7; (i = i + 2))
{
for (s = nos; s >= 1; s--)
{
printf(" ");
}
for (j = 1; j <= i; j++) {
if (j == 1 || j == i)
{
printf("*");
} else
{
printf(" ");
}
}
printf("\n");

nos--; //The spaces are in a decrementing order.
}




    return 0;
}

No comments:

Post a Comment