Thursday, June 9, 2011

C Program to print various STAR PATTERNS

1)









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

2)










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

3)
















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

4)















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

5)



















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

// SOrry for this post . Actually the page setup was not working properly . That is why the programs are having a bit of heck. But I guarantee that once you feed them inot the compiler , they will give you the required output.

Please let me know if there is any error.

No comments:

Post a Comment

Post a Comment