Wednesday, 29 August 2012

Star pattern programs in C

                                                                                                                  for looping statement go here
Q no 1.Write a C program to print the following pattern:
         *
      *  *
   *  *  *
*  *  *  *

solution

#include<stdio.h>
#include<conio.h>
void 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; 
}


Q. No 2 .Write a C program to print the following pattern:

*   *   *   *   *
  *   *   *   *
    *   *   *
      *   *
        *
      *   *
    *   *   *
  *   *   *   *
*   *   *   *   *
Solution :


#include<stdio.h>
#include<conio.h>
int main() 
{
 char prnt = '*';
 int i, j, s, nos = 0;
 for (i = 9; i >= 1; (i = i - 2)) {
  for (s = nos; s >= 1; s--) {
   printf("  ");
  }
  for (j = 1; j <= i; j++) {
   if ((i % 2) != 0 && (j % 2) != 0) {
    printf("%2c", prnt);
   } else {
    printf("  ");
   }
  }
  printf("\n");
  nos++;
 }
 nos = 3;
 for (i = 3; i <= 9; (i = i + 2)) {
for (s = nos; s >= 1; s--) {
   printf("  ");
  }
  for (j = 1; j <= i; j++) {

   if ((i % 2) != 0 && (j % 2) != 0) {
    printf("%2c", prnt);
   } else {
    printf("  ");
   }
  }
  nos--;
  printf("\n");
 }
 return 0;
}

Q. no 3.Write a C program to print the following pattern:



  • * * * * * * * * * * * * * * * * *
      * * * * * * *   * * * * * * *
        * * * * *       * * * * *
          * * *           * * *
            * * * * * * * * *
              * * * * * * *
                * * * * *
                  * * *
                    *
  • Solution :

    #include<stdio.h>
    #include<conio.h> 
    int main() 
    {
     char prnt = '*';
     int i, j, k, s, sp, nos = 0, nosp = -1;
     for (i = 9; i >= 3; (i = i - 2)) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
    printf("%2c", prnt);
    }
    for (sp = nosp; sp >= 1; sp--) {
       printf("  ");
      }
      for (k = 1; k <= i; k++) {
     if (i == 9 && k == 1) {
    continue;
    }
    printf("%2c", prnt);
    }
    nos++;
    nosp = nosp + 2;
    printf("\n");
    }
    nos = 4;
    for (i = 9; i >= 1; (i = i - 2)) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      nos++;
      printf("\n");
     }
    
     return 0;
    }


  • Q no 4.Write a C program to print the following pattern:
              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
        * * * * * * *
          * * * * *
            * * *
              *
            * * *
          * * * * *
  • Solution:
    #include<stdio.h>
    #include<conio.h>
    int main()
     {
     char prnt = '*';
     int i, j, k, s, nos = 4;
     for (i = 1; i <= 5; i++) {
    for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      for (k = 1; k <= (i - 1); k++) {
    if (i == 1) {     continue;
    }
    printf("%2c", prnt);
    }
     printf("\n");   nos--;
    }
     nos = 1;
    for (i = 4; i >= 1; i--) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      for (k = 1; k <= (i - 1); k++) {
       printf("%2c", prnt);
      }
      nos++;
      printf("\n");
     }
     nos = 3;
     for (i = 2; i <= 5; i++) {
    if ((i % 2) != 0) {
    for (s = nos; s >= 1; s--) {
        printf("  ");
       }
       for (j = 1; j <= i; j++) {
        printf("%2c", prnt);
       }
      }
      if ((i % 2) != 0) {
       printf("\n");
       nos--;
      }
     }
     return 0;
    }
    
    
    
    
    
    
                                        ........to be continued         R.P

    No comments:

    Post a Comment