Friday, November 15, 2013

Pattern

Write a program to display the following pattern in C language (Pyramid in square)?

+ + + + + * + + + + +
+       *   *       +
+     *       *     +
+   *           *   +
+ *               * +
* * * * * * * * * * *



#include<stdio.h>
int main() {

int i,j;
int n = 11;        // Value of n should be ODD

for (i = 0 ; i <= (n / 2) ; i++) {
      for (j = 0 ; j < n ; j++) {
                if (i == 0) {
                     if (j == (n / 2 ))
                              printf ("* "); 
                     else
                               printf ("+ ");
                }
                else if (i == (n / 2)) {
                     printf ("* ");
                } 
                else {
                      if (j == 0 || j == (n - 1))
                               printf ("+ ");
                      else if (j == ((n / 2) - i) || j == ((n / 2) + i))
                               printf("* ");
                      else
                               printf ("  ");
                      }
                 } 
                 printf ("\n");
      }
} 

Output:-
+ + + + + * + + + + +
+       *   *       +
+     *       *     +
+   *           *   +
+ *               * +
* * * * * * * * * * *

Note:- To increase or decrease the size of the pattern change the value of variable "n" But keep in mind that the value of n should be ODD (1,3,5,7.....)

By +Dixit Singla 

No comments: