This blog is under construction

Saturday 22 June 2013

C program to generate pascal triangle

Write a C program to generate the given pascal triangle.

               4
           3  4  3
       2  3  4  3  2
   1  2  3  4  3  2  1



  #include <stdio.h>
  int main() {
        int n, i , j, data;
        printf("Enter the no of line:");
        scanf("%d", &n);
        for (i = n; i > 0; i--) {

                data = i;

                /* space at the front of triangle */
                for (j = i; j > 0; j--) {
                        printf("   ");
                }

                /* print values of first half of triangle */
                for (j = n; j >= i; j--) {
                        printf("%3d", data++);
                }

                data = data - 2;

                /* print values of second half of triangle */
                for (j = n; j > i; j--) {
                        printf("%3d", data--);
                }

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



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



No comments:

Post a Comment