Pascal triangle numbers are very famous as an assignment. Below you will find a quik and easy solution. Soon i will post an updated version with less code though.
Pascal Triangle overview:
![]()
My code below will create this output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <stdio.h> #include <stdlib.h> int main() { int **nums,**fnums,i,c,rows,spaces; printf("Enter rows: "); scanf("%d",&rows); //create the table for the triangle numbers nums = (int **)malloc(sizeof(int *) * rows); for(i=0;i<rows;i++) *(nums+i) = (int *)malloc(sizeof(int) * rows); //zeroing the cells for(i=0;i<rows;i++) for(c=0;c<rows;c++) *(*(nums+i)+c) = 0; for(i=0;i<rows;i++){ for(c=0;c<=i;c++) if(c==0) *(*(nums+i)+c) = 1; else *(*(nums+i)+c) = *(*(nums+i)+c-1) * (i+1-c)/c; } printf("\nThe Pascal Triangle numbers!\n"); for(i=0;i<rows;i++) { putchar('\n'); if (i<10) printf("(%d) ",i); else printf("(%d) ",i); for(c=0;c<=i;c++) printf("%-7d",*(*(nums+i)+c)); } //printf("Table printed"); putchar('\n'); putchar('\n'); //creating the formated table spaces = rows * 2 - 1; fnums = (int **)malloc(rows * sizeof(int *)); for(i=0;i<rows;i++) *(fnums+i) = (int *)malloc(spaces * sizeof(int)); //zeroing the formatted table for(i=0;i<rows;i++) for(c=0;c<spaces;c++) *(*(fnums+i)+c) = 0; //puts each pascal number to the appropriate position for(i=0;i<rows;i++) for(c=0;c<=i;c++) if (c==0) *(*(fnums+i)+((rows-1)-i)) = *(*(nums+i)+c); else *(*(fnums+i)+((rows-1)-i+2*c)) = *(*(nums+i)+c); printf("\n\nPascal Triangle!\n"); printf("================\n"); for(i=0;i<rows;i++) { for(c=0;c<spaces;c++) if ( *(*(fnums+i)+c) == 0 ) printf(" "); else printf("%3d",*(*(fnums+i)+c)); putchar('\n'); } putchar('\n'); free(nums); free(fnums); return 0; } |
