Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // Function to work out a value in Pascal's trianlge for a given
  4. // row and value... nCr formula
  5. int valuePT(int n, int k) {
  6.   if (k > 0) {
  7.     // Calculating result of formula
  8.     double pt1 = (double) valuePT(n,k-1); // Recursivly call the function
  9.     double pt2 = (double) ((n + 1) - k) / k;
  10.     double result = pt1 * pt2;
  11.  
  12.     return (int) result;
  13.  
  14.   } else { // Base Case
  15.     return 1;
  16.   }
  17. }
  18.  
  19. int main(int argc, char *argv[]) {
  20.   // ASSUMPTION: The user does enter a digit between 1 and 9 and nothing else
  21.   // Gets an integer digit from a user input
  22.   char n_char[2];
  23.   printf("Enter row of Pascal's Triangle to print (1-9): ");
  24.   scanf("%s", n_char);
  25.   int n = n_char[0] - '0';
  26.  
  27.   // Loops from 0 to that input calculating the value in the triangle
  28.   // using the valuePT function and outputting it
  29.   int i;
  30.   for (i = 0; i <= n; i++) {
  31.     printf("%d  ", valuePT(n,i));
  32.   }
  33.   printf("\n");
  34.  
  35.   return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement