Friday, January 10, 2014

Printing Pattern 4( 2 Right angle triangles )

*
**
***
****
***
**
*

The pattern can seen as two right angle triangles previously discussed in  the blog.
Outer loop ( for no. of lines:7) will loop 7 times.
Inner loop should 1st increase the no. of * in pattern up till 4 lines and then decrease them.

Outer loop
for(i=1;i<=7;i++)

1st Inner loop (increasing stars upto line no.4)
  for(j=1;j<=i;j++)
                 printf("*");
For 1st loop (i=1)
j=1 , j<= i yes print *
newline

For 2nd loop(i=2)
j=1, j<=i yes print *
j=2, j<=i yes print *
newline
and so on for i= 3 and 4 prints *** and ****

 2nd Inner loop(decreasing stars after line no.4)
 for(j=i;j<=7;j++)
                 printf("*");

For 5th loop (i=5)
j=5(i) , j<= 7 yes print *
j=6(i+1) , j<= 7 yes print *
j=7(i+2),  j<=7 yes print *
newline 

For 6th loop(i=6)
j=6(i), j<=7 yes print *
j=2(i+1), j<=7 yes print *
newline 

for 7th loop (i=7)
j=7(i) , j<=7 yes print *
newline

An  if else loop to check the line no.
 if(i<=4)
{
1st Inner loop
}
else
{
2nd Inner loop
}
 
#include<stdio.h>
int main()
{
for(i=1;i<=7;i++)
     {
        if(i<=4)
        {
           for(j=1;j<=i;j++)
                 printf("*");
        }

        else
       {
           for(j=i;j<=7;j++)
                 printf("*");
        }

    printf("\n");
  }
  return 0;

}



No comments:

Post a Comment