Skip to content

Instantly share code, notes, and snippets.

@jithusunny
Created September 25, 2011 07:52
Show Gist options
  • Save jithusunny/1240363 to your computer and use it in GitHub Desktop.
Save jithusunny/1240363 to your computer and use it in GitHub Desktop.
pascal
/*Filename: pascal.c
Blog: http://jithusunnyk.blogspot.com/
Date: 24-09-11
Description: Finds out the number at any given location(row, position) of Pascal's triangle.
Row-one is conventionally enumerated as 0 & position-one as 0, position-two as 1, etc*/
#include <stdio.h>
int element_at(int row,int pos) {
if (pos == 0 || pos == row)
return 1;
return element_at(row - 1, pos - 1) + element_at(row - 1, pos);
}
int main() {
int row, pos;
printf("Enter row and position: ");
scanf("%d%d", &row, &pos);
if (pos > row)
printf("Invalid!\n");
else
printf("Number: %d\n", element_at(row, pos));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment