This blog is under construction

Saturday 22 June 2013

C program to print given pascal triangle

Write a C program to print given pascal triangle.
                  1
              2  2  2
          3  3  3  3  3
      4  4  4  4  4  4  4
  5  5  5  5  5  5  5  5  5




  #include <stdio.h>
  int main() {
        int n, i, j, k;
        printf("Enter the number of lines:");
        scanf("%d", &n);
        for (i = 1; i <= n; i++) {
                /* loop to print space at the front of triangle */
                for (j = i; j <= n; j++) {
                        printf("   ");

                }

                /* print values at front half of triangle */
                for (k = 1; k <= i; k++) {
                        printf("%3d", i);
                }

                /* print values of the second half of triangle */
                for (k = 1; k <= i - 1; k++) {
                        printf("%3d", i);
                }

                printf("\n");
        }
        return 0;
  }



  Output:
 jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of lines:5
                 1
              2  2  2
           3  3  3  3  3
        4  4  4  4  4  4  4
     5  5  5  5  5  5  5  5  5



No comments:

Post a Comment