C program to Print Pascal Triangle | C code champ

Mr Coder August 25, 2012 13




C program to print pascal triangle : In our previous code, we have seen how to draw a pyramid using C program. Now we will see how to extend our previous logic to print pascal’s triangle using C code. But first of all, we must know what is pascal triangle is ?

(From Wikipedia) In mathematics, Pascal’s triangle is a triangular array of the binomial coefficients in a triangle. The rows of Pascal’s triangle are conventionally enumerated starting with row n = 0 at the top. The entries in each row are numbered from the left beginning with k = 0 and are usually staggered relative to the numbers in the adjacent rows. A simple construction of the triangle proceeds in the following manner. On row 0, write only the number 1. Then, to construct the elements of following rows, add the number above and to the left with the number above and to the right to find the new value. If either the number to the right or left is not present, substitute a zero in its place. For example, the first number in the first row is 0 + 1 = 1, whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row.

This construction is related to the binomial coefficients by Pascal’s rule, which says that if

(x+y)^n=\sum_{k=0}^n{n \choose k}x^{n-k}y^{k}

then

 {n \choose k} = {n-1 \choose k-1} + {n-1 \choose k}

for any non-negative integer n and any integer k between 0 and n.

Now let us see how to write a C program of Pascal Triangle in C language which takes number of rows as input and then displays Pascal’s triangle.

C program to print Pascal triangle :

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

        int a[15][15], i, j, rows, num = 25, k;
        printf("----------------------------------------------------------------------\n");
        printf("-------------------made by C code champ ------------------------------\n");
        printf("----------------------------------------------------------------------\n");
        printf("\n\n\t\t C PROGRAM OF PASCALS TRIANGLE \n");  
        printf("\n Enter the number of rows you want to show : ");
        scanf("%d", &rows);
        printf("\n\n\n");
        for (i = 0; i < rows; i++) {
               for (k = num - 2 * i; k >= 0; k--)
                     printf(" ");
               for (j = 0; j <= i; j++) {
                     if (j == 0 || i == j) {
                         a[i][j] = 1;
                     } else {
                         a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
                     }
                     printf("%4d", a[i][j]);
               }
               printf("\n");
         }
         getch();
}

We hope that you all have enjoyed the above C code to Print Pascal’s triangle. If you have any issues with above code or logic ask us in form of comments.

13 Comments »

  1. shop of cheaper Hermes Birkin September 1, 2012 at 8:48 am - Reply

    Wow! Thank you! I permanently wanted to write on my website something like that. Can I implement a part of your post to my website?

    • Mr Coder September 1, 2012 at 11:15 am - Reply

      Yes sure !
      Thanks for asking..

  2. collectiva September 1, 2012 at 10:46 am - Reply

    It is very useful for the Beginners.

  3. Chung Casarz September 27, 2012 at 5:43 pm - Reply

    I simply want to tell you that I’m very new to blogging and site-building and honestly enjoyed you’re website. Likely I’m want to bookmark your blog . You absolutely have tremendous stories. Regards for sharing your web-site.

  4. black hat seo September 30, 2012 at 11:58 am - Reply

    Hi, just wanted to say, I enjoyed this blog post. It was helpful. Keep on posting!

  5. バーバリー アウトレット October 3, 2012 at 3:32 am - Reply

    Amazing things here. I’m very satisfied to peer your article. Thank you so much and I’m taking a look forward to touch you. Will you kindly drop me a mail?

  6. regim hotelier cluj October 11, 2012 at 8:15 pm - Reply

    Hi, every time i used to check web site posts here in the early hours in the break of day, since i like to find out more and more.

  7. burberry bowling bags October 13, 2012 at 4:36 pm - Reply

    Good day. Very nice web site!! Man .. Excellent .. Wonderful .. I will bookmark your site and take the feeds also…I am glad to find a lot of useful info right here in the article. Thanks for sharing..

  8. a1539919 October 22, 2012 at 2:52 am - Reply

    I’ve said that least 1539919 times. SKC was here…

  9. regim hotelier iasi October 29, 2012 at 10:37 pm - Reply

    Surfing around reddit.com I noticed your blog bookmarked as: » Blog Archive » DSC00247. Now I am assuming you book-marked it yourself and wanted to ask if social book-marking gets you a good deal of targeted visitors? I’ve been thinking of doing some social bookmarking for a few of my websites but wasn’t certain if it would produce any positive results. Many thanks.

  10. Major Dellon November 3, 2012 at 8:26 am - Reply

    Aw, this was an exceptionally nice post. Taking a few minutes and actual effort to create a very good article… but what can I say… I put things off a lot and never manage to get nearly anything done.

  11. Karri Timberman November 5, 2012 at 6:35 am - Reply

    It’s hard to search out knowledgeable folks on this subject, however you sound like you realize what you’re speaking about! Thanks

  12. ni June 16, 2014 at 12:30 pm - Reply

    j,f7utfu7igv

Leave A Response »