Advertisement
Mr_kindle

pascaltriangle.cpp

Nov 30th, 2022 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | Source Code | 0 0
  1. /*
  2.     Name: Pascaltriangle.cpp
  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. youtube: https://youtu.be/tHeyKF4iv2w
  9. */
  10.  
  11.  
  12.  
  13. #include<iostream>
  14. using namespace std;
  15.  
  16. int ncr (int, int);
  17. int factorial(int);
  18. int main()
  19. {
  20.     int n,i,j;
  21.     cout << "Enter number of rows: \n";
  22.     cin >> n;
  23.    
  24.     /*now making pascal triangle*/
  25.     for(i=0;i<n;i++)
  26.         {   /*for printing space*/
  27.             for(j=0;j<n-i;j++)
  28.                 printf(" ");
  29.             /*for printing numbers*/
  30.         /*if we condider pascal triangle as a matrix then each number printed is nCr of corresponding 'i'
  31.         and 'j'. where 'i' is corresponding roe index and 'j' is corresponding column index*/
  32.             for(j=0;j<=i;j++)
  33.                 printf("%d ",ncr(i,j));
  34.             printf("\n");
  35.                
  36.            
  37.         }
  38. return 0;
  39. }//main
  40.  
  41. int ncr (int i, int j)
  42.     {
  43.         int num;
  44.         num = factorial(i)/(factorial(i-j) * factorial(j));
  45.         return num;
  46.     }//end of ncr
  47.    
  48. int factorial(int n)
  49.     {
  50.         int prod=1,i;
  51.         for(i=1;i<=n;i++)
  52.             prod = prod * i;
  53.        
  54.         return prod;
  55.     }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement