Sunday, 7 October 2012

Pattern Programs


Program to print pyramid pattern in C

/*Pattern 1 :
    * * * * *
    * * * * *
    * * * * *
    * * * * *
    * * * * *
Author : Bhavin Tank */

#include <stdio.h>
#include <conio.h>
void main()
{
               int i,j;
               clrscr();
               for(i=0; i<5; i++)
               {
                               for(j=0; j<5; j++)
                               {
                                               printf(" * ");
                               }
                                printf("\n");
               }
               getch();
}

/*           Pattern 2
               *
               * *
               * * *
               * * * *
Author : Bhavin Tank */

#include <stdio.h>
#include <conio.h>
void main()
{
               int i,j,no;
               clrscr();
              
               printf(“How many lines you want : ”);
               scanf(“%d”,&no);
               for(i=0; i<no; i++)
               {
                               for(j=0; j<=i; j++)
                               {
                                              printf(" * ");
                               }
                               printf("\n");
               }
               getch();
}

/*           Pattern 3
      *
    * *
  * * *
* * * *
Author : Bhavin Tank */

#include <stdio.h>
#include <conio.h>
void main()
{
               int i,j,k,no;
               clrscr();
              
               printf(“How many lines you want : ”);
               scanf(“%d”,&no);

               for(i=1; i<=no; i++)
               {
                               for(j=5; j>=i; j--)
                               {
                                              printf(" ");
                               }
                               for(k=1; k<=i; k++)
                               {
                                              printf("*");
                               }
                               printf("\n");
               }
               getch();
}

/* Pattern 4

* * * * *
* * * *
* * *
* *
*
Author : Bhavin Tank */

#include <stdio.h>
#include <conio.h>
void main()
{
               int i,j,no;
               clrscr();
              
               printf(“How many lines you want : ”);
               scanf(“%d”,&no);

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

/* Pattern 5
* * * * *
  * * * *
    * * *
      * *
        *
Author : Bhavin Tank */

#include <stdio.h>
#include <conio.h>
void main()
{
               int i,j,k,space=1;
               clrscr();

               printf(“How many lines you want : ”);
               scanf(“%d”,&no);

               for(i=no; i>=1; i--)
               {
                               for(k=space; k>=0; k--)
                               {
                                              printf(" "); // only 1 space
                               }
                               for(j=i; j>=1; j--)
                               {
                                              printf("*");
                               }
                               space = space + 1;
                               printf("\n");
               }
               getch();
}

/* Pattern 6

   *
  * *
 * * *
* * * *

Author : Bhavin Tank */

#include <stdio.h>
#include <conio.h>
void main()
{
               int i,j,k,t=0;
               clrscr();

               printf(“How many lines you want : ”);
               scanf(“%d”,&no);

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

/* Pattern 7
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Author : Bhavin Tank */

#include <stdio.h>
#include <conio.h>
void main()
{
               int i,j,k,space=1;
               clrscr();
               for(i=1; i<=5; i++)
               {
                               for(k=space; k<=5; k++)
                               {
                                     printf(" ");
                               }
                               for(j=0; j< i; j++)
                               {
                                     printf("*");
                               }
                               space = space + 1;
                               printf("\n");
               }
               space = 1;
               for(i=4; i>=1; i--)
               {
                               for(k=space; k>=0; k--)
                               {
                                      printf(" ");
                               }
                               for(j=i; j>=1; j--)
                               {
                                   printf("*");
                               }
                               space = space + 1;
                               printf("\n");
               }
               getch();
}


/*Pattern 7


// Write a program to print the double dimond   
    *         *
   * *     * *
  * * *   * * *
 * * * * * * * *
  * * *   * * *
   * *     * *
    *       *  

 */


#include<stdio.h>

#include<conio.h>
void main()
{
    int i,j,s,n;
    clrscr();

    printf("Enter the number :");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(s=n-i;s>=1;s--)
            printf(" ");

        for(j=1;j<=i;j++)
            printf("* ");

        for(s=n-i;s>=1;s--)
            printf("  ");
        for(j=1;j<=i;j++)
            printf("* ");


        printf("\n");
    }
    for(i=n-1;i>=1;i--)
    {
        for(s=1;s<=n-i;s++)
            printf(" ");

        for(j=1;j<=i;j++)
            printf("* ");

        for(s=1;s<=n-i;s++)
            printf("  ");

        for(j=1;j<=i;j++)
            printf("* ");
        printf("\n");
    }
    getch();
}
Click Here to Download

/* Pattern 8

*              *
* *         * *
* * *     * * *
* * * * * * * *
* * *     * * *
* *         * *
*             *
Author : Bhavin Tank */

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

    printf("Enter the no : ");
    scanf("%d",&no);
    for(i=1;i<=no;i++)
    {
        for(j=1;j<=i;j++)
            printf("* ");
        for(s=1;s<=no-i;s++)
            printf("    ");

        for(j=1;j<=i;j++)
            printf(" *");

        printf("\n");
    }
    for(i=no-1;i>=1;i--)
    {
        for(s=i;s>=1;s--)
            printf("* ");
        for(s=1;s<=no-i;s++)
            printf("    ");
        for(j=1;j<=i;j++)
            printf(" *");
        printf("\n");

    }
    getch();
}
Click Here to Download

No comments:

Post a Comment