Sunday, July 24, 2011

Some Pattern's in C

1). Write a C program to print the following pattern:

         *
      *  *
   *  *  *
*  *  *  *

Program:
 
/* This is a simple mirror-image of a right angle triangle */

#include 
int main() {
 char prnt = '*';
 int i, j, nos = 4, s;
 for (i = 1; i <= 5; i++) {
for (s = nos; s >= 1; s--) {  // Spacing factor
   printf("  ");
  }
  for (j = 1; j <= i; j++) {
   printf("%2c", prnt);
  }
  printf("\n");
  --nos;   // Controls the spacing factor
 }
 return 0;
}


  • Write C program to print the following pattern:


  • *                   *
    * * *           * * *
    * * * * *   * * * * *
    * * * * * * * * * * *

    Program:

    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, c = 1, nos = 9;
     for (i = 1; c <= 4; i++) {
         // As we want to print the columns in odd sequence viz. 1,3,5,.etc
      if ((i % 2) != 0) {
       for (j = 1; j <= i; j++) {
     printf("%2c", prnt);
    }
    for (s = nos; s >= 1; s--) { //The spacing factor
        if (c == 4 && s == 1) {
         break;
        }
        printf("  ");
       }
       for (k = 1; k <= i; k++) {
        if (c == 4 && k == 5) {
         break;
        }
        printf("%2c", prnt);
       }
       printf("\n");
       nos = nos - 4;  // controls the spacing factor
       ++c;
      }
     }
     return 0;
    }


    Write C program to print the following pattern:


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

    Program:

    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, p, r, nos = 7;
    
     for (i = 1; i <= 5; i++) {
      for (j = 1; j <= i; j++) {
     if ((i % 2) != 0 && (j % 2) != 0) {
    printf("%3c", prnt);
    }
    else if ((i % 2) == 0 && (j % 2) == 0) {
    printf("%3c", prnt);
    }
    else {
    printf("   ");
    }
    }
    for (s = nos; s >= 1; s--) {  // for the spacing factor
       printf("   ");
      }
      for (k = 1; k <= i; k++) { //Joining seperate figures
    if (i == 5 && k == 1) {
     continue;
    }
    if ((k % 2) != 0) {
    printf("%3c", prnt);
    }
    else {
    printf("   ");
    }
    }
    printf("\n");
    nos = nos - 2;   // space control
    }  
    nos = 1; // remaining half..
    for (p = 4; p >= 1; p--)
    {
    for (r = 1; r <= p; r++)
    {
    if ((p % 2) != 0 && (r % 2) != 0)
    {
    printf("%3c", prnt);
    }
    else if ((p % 2) == 0 && (r % 2) == 0)
    {
    printf("%3c", prnt);
    }
    else
    {
    printf(" ");
    }
    }
    for (s = nos; s >= 1; s--)
    {
    printf(" ");
     for (k = 1; k <= p; k++)
    {  
    if ((k % 2) != 0)
     {
    printf("%3c", prnt);
    }
    else
     {   
     printf(" ");
    }
     nos = nos + 2; // space control
    printf("\n");
    }
    return 0;
    }

    No comments:

    Post a Comment