Ask a Teacher



Write a program to display the given pattern: 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1

#include<stdio.h>
int main() {
      /* c taken for columns */
      int i, j, c = 9, m, k;
      for (i = 1; i <= 5; i++) {
            /* k is used for spaces */
            for (k = 1; k <= c; k++) {
                  printf(" ");
            }
            for (j = 1; j <= i; j++) {                   printf("%2d", j);             }             for (m = j - 2; m > 0; m--) {
                  /* %2d ensures that the number
                   * is printed in two spaces
                   * for alignment */
                  printf("%2d", m);
            }
            printf("\n");
            /* c is decremented by 2 */
            c = c - 2;
      }
      return 0;
}


comments powered by Disqus