-->

program to display following star pattern

//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");
 }
 getch();

 }
------------------------------
logic in mind
----------------------
1.)The given pattern has two parts.
          *
       *
  *
*
and 
             *
                *
                   *
                        *
so for these two, we use two loops with spaces between them when we combine them.
2)since there are five asterisks we display using loop with decreasing spaces(see figure).
3)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.
---------------------------------------------------------------------------------------
more about this logic , you can understand from following screen shot.

output screenshot is:-

No comments:

Post a Comment