Skip to content

Instantly share code, notes, and snippets.

@decal
Last active April 27, 2018 07:48
Show Gist options
  • Save decal/27dfb03f6183522ad7958384f02dca68 to your computer and use it in GitHub Desktop.
Save decal/27dfb03f6183522ad7958384f02dca68 to your computer and use it in GitHub Desktop.
🔺 A Pascal's Triangle Program in C11
/*
* Calculate the binomial coefficients required to represent Pascal's Triangle
* after prompting the user for a tree depth value..
*
* Derek Callaway [decal (AT) sdf {D0T} org] Tue Jul 4 11:37:30 PDT 2017
*
* Compile with: gcc -std=c11 -pedantic -ansi -o pascals-triangle pascals-triangle.c
*/
#include<stdio.h>
#include<stdlib.h>
static unsigned long fact(const unsigned long) __attribute__ ((fastcall, visibility("internal")));
signed int main(void) {
register unsigned long i = 0;
auto unsigned long line = 0;
fputs("*** Enter tree depth as a whole number (i.e. no of lines in triangle): ", stdout);
scanf("%lu", &line);
for(;i < line;++i) {
const unsigned long b = line - i - 1;
register unsigned long j = 0;
for(;j < b;++j)
putchar(' ');
for(j = 0;j <= i;++j) {
const unsigned long n = fact(i) / (fact(j) * fact(i - j));
printf("%lu ", n);
}
putchar('\n');
}
_Exit(EXIT_SUCCESS);
}
static unsigned long fact(const unsigned long num){
register unsigned long r = 1, i = 1;
while(i <= num)
r *= i++;
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment