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

C program to print hollow pyramid star pattern

/**
 * C program to print hollow pyramid  star pattern

                *
             *     *
           *           *
        *                 *
      *                      *
 
*/

#include <stdio.h>
#include<conio.h>
void main()
{
    int i, j, rows;

    /* Input rows to print from user */
    printf("Enter number of rows : ");
    scanf("%d", &rows);

    for(i=1; i<=rows; i++)
    {
        /* Print trailing spaces */
        for(j=i; j<rows; j++)
        {
            printf(" ");
        }

        /* Print hollow pyramid */
        for(j=1; j<=(2*i-1); j++)
        {
            /*
             * Print star for last row (i==rows),
             * first column(j==1) and for
             * last column (j==(2*i-1)).
             */
            if(j==1 || j==(2*i-1))
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        /* Move to next line */
        printf("\n");
    }

    getch();

}
source: codeforwin.org

hseb16 sol:program to get given star pattern

/*program to get following pattern
      * * * * *
      * * * *
      * * *
      * *
      * 
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=5;i>=1;i--)
  {
     for(j=1;j<=i;j++)
          {
                printf("* ");
          }
       printf("\n");
 }
getch();
}

Write a program to get given pattern

/*WAP to get following
                                  * * * * * 
                        * * * * *
                   * * * * * 
             * * * * *
*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k;
for(i=1;i<=5;i++)      //for number of rows. there are five rows so loop moves fives times
  {
    for(k=8;k>=i;k--)
      {
      printf(" ");   // this part is for space. every time , on display, it decreases space.
      }
    for(j=1;j<=4;j++) //it displays star five times.It displays in columns
      {
 printf("* ");
      }
    printf("\n"););
}
getch();
}
---------------------------
logic in mind:-
----------
it has three parts:
first is number of rows
second part is number of columns
third part decreasing space on each display.
1)fist loop is used for number of rows
2) the inner loop is for space to display
3)the inner most is for star display every time.

Program's screen:-


program's output:


Note:-
From here onward, we are writing program using return and int main().
so using return, it looks like.
#include <stdio.h>
#include<conio.h>
int main()
{
  int i,j,k;
for(i=1;i<=5;i++)      //for number of rows. there are five rows so loop moves fives times
  {
    for(k=8;k>=i;k--)
      {
      printf(" ");   // this part is for space. every time , on display, it decreases space.
      }
    for(j=1;j<=4;j++) //it displays star five times.It displays in columns
      {
 printf("* ");
      }
    printf("\n");
}
   getch();
  return 0;
}
---------------------------------------------------
read me:-

nothing you have to do here. Just put int main() in the beginning and return 0 at the end

Write a program to get following numeric pattern.

/*Write a program to get following numeric pattern.
0
01
012
0123                                                                                                                    
012
01
0
*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=0;i<=3;i++)
{
 for(j=0;j<=i;j++)
   {
      printf("%d",j);

   }
      printf("\n");
}
for(i=2;i>=0;i--)
{
 for(j=0;j<=i;j++)
   {
      printf("%d",j);

   }
      printf("\n");
}
getch();
}
-------------------------------------------------
logic in mind:-
1) we have to display given pattern.It has two parts
      first part is:upper part .i..e
        0
       01
       012
        0123   
  and second part is
     012
     01
     0
2)for first part, like shown below, we use loop to fetch value from outer loop and displaying from inner loop
3)for second part, we use once again loop with one less value. We fetch that and display from inner loop
just look down.

program's screenshot:

----------------------------
output screen:



program to display following star pattern.

 /*program to display following star pattern.
         * * * * * *
           * * * *
            * * *
             * *
              *
             *
            * *                                                                                                           
           * * *
         * * * *
       * * * * * 
*/
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int i,j,k,sp=1;
//printf("for upper part");
 for(i=1;i<=5;i++)
   {
for(k=sp;k<=i;k++)
  {
    printf(" ");

     }
    for(j=5;j>=i;j-- )
      {
printf(" *");
    }
    printf("\n");
}
 //printf("for lower part");

 sp=5;
 for(i=1;i<=5;i++)
   {
for(k=sp;k>=i;k=k-1)
  {   printf(" ");

     }
    for(j=1;j<=i;j++)
      {
      printf(" *");
    }
    printf("\n");
}
getch();

}
-------------------------------
logic in mind:-
---------------------------------
1)First loop is for number of rows.
2)like other programs, we display first upper part. 
       For this, we, first, display space (second loop)and then star(third loop.
3)For second part, we display first space(look diagram) using loop then we display star.
------------------------------------------------------------------------------------------------------------------------------
program's screenshot:-


programs output:-



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:-

program to display following numeric/star pattern.

//program to display following numeric/star pattern.
//     11111
//     2222
//     333
//     44
//     5            
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;<=5;i++)
  {
     for(j=5;j>=i;j--)
          {
                printf("%d",i);
          }
       printf("\n");
 }
getch();
}
--------------------------------------------------------
logics in mind:
----------------------
1)we have to display 1,then 2222, then 333,so on.  we use loop for this.
 2)since the displays are in repetitive form ,,we set up the loop in nested form such that
        internal loop executes first  time with display of numbers(above) which it takes from outer loop,then 
four times  and it goes on decreasing.
3)First loop(outer) takes value 1 and assigns to inner.The inner executes five times with display taken from outer loop.
4)next time, the outer loop takes the value 2 and transfers to inner loop, it displays number taken from outer loop again and again.and it goes on.
............................................................................................
if you want to display "*" then use "*" in printf function.

program to display following numeric/star pattern.

//program to display following numeric pattern.
//      1
//      12
//      123
//      1234
//      12345 
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
  {
     for(j=1;j<=i;j++)
          {
                printf("%d",j);
          }
       printf("\n");
 }
getch();
}
--------------------------------------------------------
logics in mind:
----------------------
1)we have to display 1,then 12, then 1233,so on.  we use loop fo this.
 2)since the displays are in repetitive form ,,we set up the loop in nested form such that
        internal loop executes first one  time with display of numbers(above),then two times  and it goes on increasing.
3)First loop(outer) takes value 1 and assigns to inner.The inner executes one times with display.
4)next time, the outer loop takes the value 2 and transfers to inner loop, it displays again and again.
and it goes on
............................................................................................
if you want to display "*" then use "*" in printf function.

program to display star/number pattern

//program to display given star/number pattern
//      12345
//        1234
//          123
//            12
//              1
#include<stdio.h>
#include<stdio.h>
void main()
{
clrscr();
int i,j,k,sp=5;
 for(i=5;i>=1;i--)
   {
               for(k=sp;k>=i;k--)
                 {
                  printf(" ");
                  }
                        for(j=1;j<=i;j++)
                           {
                            printf("%d",j);
                          }
           printf("\n");
  }
getch();
}
---------------------------------
logics in mind
-------------------------------
1)we have to display 1,2,3,4,5 so we use loop.
 2)since the displays are in repetitive form ,,we set up the loop in nested form such that
        internal loop executes 5 times with display of numbers(above).
3)if we look carefully to given pattern we can see there space increased after each display. for this, we have used loop. in each display, it increases space and then it goes for next display.
3)First loop(outer) takes value 5 and assigns to inner.The inner executes 5 times with display.
4)next time, the outer loop takes the value 4 and transfers to inner loop, it displays again and again.
and it goes on

...................................................
if you wan to display star then put "*" in printf


Write a program to display following star pattern.

//Write a program to display following star pattern.
/*
              *
            **                                                                                                           
          ***
        ****
      *****
*/
#include <stdio.h>
#include<conio.h>
void main()
{
    int i,j,k;
    for(i=1;i<=5;i++)
    {
        int k;
        for(k=i;k<=5;k++)
        {
            printf(" ");
        }
        for(j=1;j<=i;j++)
        {
            printf("*");
        }
        printf("\n");
    }
    getch();
}