Creating Pascals Triangle in C, C++.

Posted on 2006-04-02
Last Modified: 2011-09-20
Ok so I have to create a Pascal triangle using a 10*10 Matrix, and the rows and columns displayed will be determined by user input. Obviously the number can't be larger than 10. I need two that will fill in the correct values to the matrix and another for the user to input the size (less than 10). The matrix should look like this

user input = 4                                                
1      1      1      1
1      2      3      4
1      3      6      10
1      4      10      20

So its a little different than the typical pascal's triangle. This is what I have so far...its the only way I could figure out how to do it, and it prints nicely with a nice triangle shape....but that's not the way "we're supposed to do it." I'm thinking for the first row and column I can create a single loop to place 1's in the top row and another for the first column. Any help is really appreciated.

void main()

int line,c,n,x;
void pasc(int);

printf("\n\nEnter the no. of rows: ");

printf("\nPascal's triangle :\n");

            printf("   ");
printf("  1\n\n");

            printf("   ");


void pasc(int n)
      int r;
 long fact(int);
                  printf("%3ld   ",fact(n)/(fact(n-r)*fact(r)));
long fact(int v)

Question by:jonmclean2
    1 Comment
    LVL 100

    Accepted Solution

    Since this is probably a school assignment I will provide a basic algorithm

    Declare a 2-dimensional array 11x11
    Initialize all cells to 0
    Set Array[0][1] to 1
    Get the number of rows
    Fill row1 - row number
    Cell[J][K] = Cell[J-1][K] + Cell[J][K-1]

    Print the array


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 Specialist

    HTML5 development skills are critical to all developers. HTML5 is the foundation to almost any development process. That's why business, design studios, development shops and other organizations need HTML5 developers. Get your foot in the door as a HTML5 specialist.

    This article is filled with multiple code samples and explanations for mathematical calculations. They are as follows: 1. General tips 2. Quadratic formula 3. Object collision 4. Projectile path General Tips       Here are some of my tips f…
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now