#include <stdio.h>
  /* Program to print out Pascal's triangle */

int pascal_num (int,int);
/* Function to print rows and columns from pascal's triangle */

int main(int argc, char **argv)
{
    int i;
    int j;
    int max;   // Maximum row of triangle to print

    max= 6;
	printf ("Pascal 5,3 is %d\n",pascal_num(5,3));
    for (i= 1; i <= 6; i++) {  /* i loops from 1 to 6 */

        for (j=1; j <= i; j++) {  /* j loops from 1 to i */
            printf ("%d ",pascal_num (j,i));
               /* Print the jth number in the ith row */
        }
        printf ("\n");  /* Print a newline at the end of each line */
    }
    
    return 0;
}

int pascal_num (int n, int r)
/* routine returns the n th number in the r th row */
{
    int pnum;
	if (r <= 0 || n <= 0 || n > r) {
		printf ("Erroneous number entered\n");
		return 0;
	}
    if (r == n || n == 1)  /* if this number is the first or the last in the
                        row then it must be 1 so simply return 1*/
        return 1;
   /* Otherwise this number is the sum of the nth and n-1 th numbers
         in the row above - calculate this sum */
    pnum= (pascal_num (n, r-1) + pascal_num (n-1, r-1) );
   /* return the sum */
    return pnum; 
}

