-->
Showing posts with label triangle pattern. Show all posts
Showing posts with label triangle pattern. Show all posts

program to display following star pattern/triangle

//program to display following star pattern/triangle
//program to display following star pattern
//              *
//           *     *                                                                                                    
//        *            *
//     *                   *
//  *                          *

//* * * * * * * * * * * * * *
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,k,m,sp=7;
for(i=1;i<=5;i++)
 {
     for(k=sp;k>=i;k--)
     {
       printf(" ");
     }
for(m=i;m<= i;m++)
{
printf("*");
}
 for(m=1;m<i;m++)
 {
 printf("  ");
 }
   for(m=i;m<=i;m++)
    {
      if(m==1)
      printf(" ");
      else
      printf("*");

    }
printf("\n");
}

 for(int i1=1;i1<=5;i1++)
 {

   printf(" * ");
 }

 getch();
 }
------------------------------
logic in mind
----------------------
1.)The given pattern has two parts.
          *
       *
  *
*
second 
             *
                *
                   *
                        *

and third is
 * * * * *  * * *
so for these three, we use three loops with spaces between them when we combine them.
1)since there are five asterisks we display using loop with decreasing spaces(see figure).First loop is used for number of rows used.Second loop is for space(decreasing)

2)third loop is displaying star.
3)the loop inside is for space and for right pattern display.then we display second part with increasing space between them. After that we display second part with next loop as shown below(picture).
4) In second part we use 'if' because we have to display first space rather asterisk so.

5)Now, left is last row.For this, we have used another loop as shown in picture given downside. 
---------------------------------------------------------------------------------------
more about this logic , you can understand from following screenshot.

-----------------------------------------------------------------------------------------------
program screenshot:-

output screenshot:-