Advertisement
Mr_kindle

pascaltriangle.c

Nov 30th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | Source Code | 0 0
  1. /*
  2.     Name: Pascaltriangle.c
  3.     Copyright:
  4.     Author: Mr.Kindle
  5.     Date: 30-11-22 21:44
  6.     Description: This code print the pascal triangle of given number of rows.
  7. */
  8.  
  9.  
  10.  
  11. #include<stdio.h>
  12.  
  13. int ncr (int, int);
  14. int factorial(int);
  15. int main()
  16. {
  17.     int n,i,j;
  18.     printf("Enter number of rows: ");
  19.     scanf("%d",&n);
  20.     /*now making pascal triangle*/
  21.     for(i=0;i<n;i++)
  22.         {   /*for printing space*/
  23.             for(j=0;j<n-i;j++)
  24.                 printf(" ");
  25.             /*for printing numbers*/
  26.         /*if we consider pascal triangle as a matrix then each number printed is nCr of corresponding 'i'
  27.         and 'j'. where 'i' is the corresponding row index and 'j' is the corresponding column index*/
  28.             for(j=0;j<=i;j++)
  29.                 printf("%d ",ncr(i,j));
  30.             printf("\n");
  31.                
  32.            
  33.         }
  34. return 0;
  35. }//main
  36.  
  37. int ncr (int i, int j)
  38.     {
  39.         int num;
  40.         num = factorial(i)/(factorial(i-j) * factorial(j));
  41.         return num;
  42.     }//end of ncr
  43.    
  44. int factorial(int n)
  45.     {
  46.         int prod=1,i;
  47.         for(i=1;i<=n;i++)
  48.             prod = prod * i;
  49.        
  50.         return prod;
  51.     }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement