Saturday, January 14, 2012

Star pattern program in c source code

1.Print a pattern
         *
      *  *
   *  *  *
*  *  *  *
To print this pattern we have to use three for loop.Inside the first loop there 
will be two loop, one is for controlling the spacing factor and another for 
printing the stars.Constant value of First loop is used in second inner loop to
decided the number of stars.
Here is the source code copy and paste it on code::blocks and run the program you
will see the result.
Source code
#include<stdio.h>
int main() {
 char star = '*';
 int i, j, num = 4, spc;
 for (i = 1; i <= 5; i++) {
     for (spc = num; spc >= 1; spc--) {  // Spacing factor
           printf("  ");
  }
  for (j = 1; j <= i; j++) {
       printf("%2c", star);
  }
  printf("\n");
  --num;   // Controls the spacing factor
 }
 return 0;
}

2.Print a pattern
*                   *
* * *           * * *
* * * * *   * * * * *
* * * * * * * * * * *
This pattern is also simple just joining two triangle in a little different way.Here odd number 
of star is printed from first and last position.
#include<stdio.h>
int main()
{
 //char star = '*';
 int i,j;
 for (i=1;i<=7;i++)
 {
     for(j=0;j<11;j++)
     {
         if(i%2!=0)
         {
         if(j<= i-1||j>=11-i)
          printf("* ");
         else
          printf("  ");
         }

     }
     printf("\n");
 }



3.Print a pattern

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

/*DISPLAYING THE DIAMOND
                    *
                  ***
                 *****
                *******
                 *****
                  ***
                    *
                    */
#include<stdio.h>
int main()
{
    int i,j,l,n,k=0;
    printf("Enter the rows");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(l=n;l>i;l--)
        {
            printf(" ");
            }
        for(j=1;j<=i+k;j++)
        {
            printf("*");
            }
        k++;
        printf("\n");
        }
     int k1=n-2;
     for(i=n-1;i>=1;i--)
     {
        for(l=i;l<=n-1;l++)
        {
            printf(" ");
            }
        for(j=i+k1;j>=1;j--)
        {
            printf("*");
            }
        k1--;
        printf("\n");
}return 0;
}

4.Print a pattern fish

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

#include<cstdio>
int main()
{
    int i,j,k,h,m,n;
    printf("Please enter the rows of fish\n");
    scanf("%d",&n);
    //PRINTING THE UPWARD PART
    for(i=n;i>=1;i--)
    {
        for(j=i;j>1;j--)
        {
            printf(" ");
            }
        for(k=n;k>=i;k--)
        {
            if(k==i) printf("*");
            else printf("* ");
            }
         for(h=i;h>1;h--)
         {
            printf("  ");
                }
        for(m=n;m>=i;m--)
        {
            printf("*");
            }
        printf("\n");
        }
    //PRINTING THE DOWNARD PART
    for(i=1;i<=n-1;i++)
     {
        for(j=1;j<i;j++)
        {
            printf(" ");
            }
        for(k=n-1;k>=i;k--)
        {
            printf(" *");
            }
        for(h=1;h<=i;h++)
        {
            printf("  ");
            }
        for(m=i;m<=n-1;m++)
        {
            printf("*");
            }
        printf("\n");
        }return 0;
    }

5.Print a pattern

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

#include<stdio.h>
int main()
{
    int i,j,n;
    printf("Please enter the rows for the right andled triangle \n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("*");
            }
        printf("\n");
        }
    for(i=n-1;i>=1;i--)
    {
        for(j=1;j<=i;j++)
        {
            printf("*");
            }
        printf("\n");
        }
        return 0;
    }
 
 

6.Print a pattern

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

#include<stdio.h>
int main()
{
    int i,j,k,n;
    printf("Please enter the rows for the right andled triangle \n");
    scanf("%d",&n);
    for(i=n;i>=1;i--)
    {
        for(k=1;k<i;k++)
        {
            printf(" ");
            }
        for(j=n;j>=i;j--)
        {
            printf("*");
            }
        printf("\n");
        }
    for(i=1;i<=n-1;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf(" ");
            }
        for(k=n-1;k>=i;k--)
        {
            printf("*");
            }
        printf("\n");
        }
        return 0;
    }


7.Print a pattern

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

#include<stdio.h>
int main()
{
    int i,j,k,n;
    printf("Please enter the rows for the right andled triangle \n");
    scanf("%d",&n);
    for(i=n;i>=1;i--)
    {
        for(k=n;k>i;k--)
        {
            printf(" ");
            }
        for(j=1;j<=i;j++)
        {
            printf("*");
            }
        printf("\n");
        }
    for(i=2;i<=n;i++)
    {
        for(k=i;k<n;k++)
        {
            printf(" ");
            }
        for(j=1;j<=i;j++)
        {
            printf("*");
            }
        printf("\n");
     }
     return 0;
}


8.Print a pattern

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

#include<stdio.h>
int main()
{
    int i,j,n;
    printf("Please enter the rows for the right andled triangle \n");
    scanf("%d",&n);
    for(i=n;i>=1;i--)
    {
        for(j=1;j<=i;j++)
        {
            printf("*");
            }
        printf("\n");
        }
    for(i=2;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("*");
            }
        printf("\n");
     }
     return 0;
}


9.Print a pattern

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

#include<stdio.h>/*calling the header files*/
int main()
{
    int i,j,k,n;  /*intializing the variables which are needed*/
    printf("Please enter the rows for the right andled triangle \n");
    scanf("%d",&n);
    /*Loop for the uppart part of the pattern*/
    for(i=n;i>=1;i--)
    {
        for(j=1;j<=i;j++)
        {
            printf("*");
            }
        for(k=n;k>i;k--)
        {
            printf("  ");
            }
        for(j=1;j<=i;j++)
        {
            printf("*");
            }
        printf("\n");
        }
    /*loop for lower part */
    for(i=2;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("*");
            }
      for(k=i;k<n;k++)
        {
            printf("  ");
            }
        for(j=1;j<=i;j++)
        {
            printf("*");
            }
        printf("\n");
     }
     return 0;
}


10.Print a pattern

      ABCDEEDCBA
      ABCD     DCBA
      ABC         CBA
      AB             BA
      A                  A
      AB             BA
      ABC         CBA
      ABCD     DCBA
      ABCDEEDCBA

#include<stdio.h>/*calling the header files*/
int main()
{
    int i,j,k,n,c,c1=0;  /*intializing the variables which are needed*/
    printf("Please enter the rows for the right andled triangle \n");
    scanf("%d",&n);
    /*Loop for the uppart part of the pattern*/
    for(i=n;i>=1;i--)
    {
        c=64;
        for(j=1;j<=i;j++)
        {
            printf("%c",c+j);
            }
        for(k=n;k>i;k--)
        {
            printf("  ");
            }
        c=65+n-c1;
        for(j=1;j<=i;j++)
        {
            printf("%c",c-j);
            }
        c1++;
        printf("\n");
        }
    /*loop for lower part */
    c1=2;
    for(i=2;i<=n;i++)
    {
        c=64;
        for(j=1;j<=i;j++)
        {
            printf("%c",c+j);
            }
        for(k=i;k<n;k++)
        {
            printf("  ");
            }
        c=65+c1;
        for(j=1;j<=i;j++)
        {
            printf("%c",c-j);
            }
        c1++;
        printf("\n");
     }
     return 0;
}

18 comments:

  1. *****
    *---*
    *---*
    *---*
    *****
    can u write the logic for this pattern

    ReplyDelete
    Replies
    1. int row,i,j;
      printf("Enter the row:");
      scanf("%d",&row);
      for(i=1;i<=row;i++)
      {
      for(j=1;j<=row;j++)
      if(i==1||i==row||j==1||j==row)
      printf("*");
      else
      printf("_");
      printf("\n");
      }

      Delete
  2. i want code for

    1
    * *
    2 1 2
    * * * *
    3 2 1 2 3

    ReplyDelete
    Replies
    1. int row,i,j,n=1,flag=1;
      printf("Enter the row:");
      scanf("%d",&row);
      for(i=1;i<=row;i++)
      {
      for(j=1;j<=i;j++)
      {
      if(i%2==0)
      printf("*");
      else if(flag)
      {
      printf("%d",n--);
      if(n==0)
      {
      flag=0;
      n=1;
      }
      }
      else
      printf("%d",++n);
      }
      if(i%2==0)
      n++;
      flag=1;
      printf("\n");
      }

      Delete
  3. Hello to everyone.......i have a question please if any one know this question's answer then please reply me or send me using my email id :: verma.ram484@gmail.com or you can also goto my blog and you can send me message using my contact us form :: http://www.technotrickies.com
    question is ::::

    1 1 1 0 1 1 1
    1 1 0 1 1
    1 0 1
    0


    please send me solution .....if any one known.....

    ReplyDelete
  4. int n;
    scanf("%d",&n);
    int i,k;
    for(k=n-1;k>=0;k--)
    {
    for(i=0;i<k;i++)
    printf("1");
    printf("0");
    for(i=0;i<k;i++)
    printf("1");
    printf("\n");
    }

    ReplyDelete
  5. 1
    2*3
    4*5*6
    7*8*9*10

    I want the code for this pattern

    ReplyDelete
    Replies
    1. int row,i,j,n=1;
      printf("Enter the row:");
      scanf("%d",&row);
      for(i=1;i<=row;i++)
      {
      for(j=1;j<=2*i-1;j++)
      if(j%2==0)
      printf("*");
      else
      printf("%d",n++);
      printf("\n");
      }

      Delete
  6. 1
    1
    1 1 2 1 1
    1
    1

    plase code

    ReplyDelete
    Replies
    1. int row=5,i,che,k;
      che=(row/2)+1;
      for(i=1;i<=row;i++)
      {

      if(i==che)
      {
      for(k=1;k<row;k++)
      {

      if(k!=che)
      printf("%d",1);
      else
      printf("%d"(row/2));
      }
      printf("\n%d",1);
      }
      else
      printf("\n%d",1);

      }

      Delete
  7. plz send me the solution of

    -----------*--------------
    -------*******------------
    ------***********----------
    -----************----------
    -----************-----------
    -----**********-----------
    ------*******----------
    ----****-------
    ---*--------

    ReplyDelete
  8. -----------*--------------
    -------*******------------
    ------*********----------
    -----***********----------
    -----**********-----------
    -------*******-------------
    ---------****-------------

    ReplyDelete
  9. #include
    #include
    void drawpattern(int w,int h,int radius)
    {
    int i,j,hw=w/2,hh=h/2;
    for(j=0;j<h;++j)
    {
    for(i=0;i<w;++i)
    {
    ??????????
    }
    }
    }
    int main(){
    drawpattern(15,15,7);
    return 0;
    }

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. I want code for..
    *
    * * *
    * * * *
    * * * * *
    *
    * * *
    * * *

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. i want to code for this

    *
    - * *
    - - * * *
    - - - * * * *

    ReplyDelete
  14. int row,i,j;
    scanf("%d",&row);
    for(i=1;i<=row;i++)
    {
    for(j=1;j<=2*i-1;j++)
    {
    if(j<=i&&j%i!=0)
    printf("-");
    else
    printf("*");
    }
    printf("\n");
    }
    }

    ReplyDelete