Skip to main content

C++ Program to Print Pascal's Triangle


Pascal's Triangle


#include<iostream> 
 using namespace std;

unsigned int factorial(unsigned int const num){

  if( num <=1 )
    return 1;
  else
    return num*factorial(num-1);
}

int main( void ){

  int row = 0;
    
  cout<<"Enter the number of rows: ";
  cin>>row;
  
  //Method 1

  for(unsigned int n=0;n<row;++n){

    //spacing before row depends on how many rows there are
    //row-i = number of space needed before row starts
    
    for(unsigned int j=1;j<(row-n);++j){
      cout<<"  ";
    }

    unsigned int n_factorial = factorial(n);
    //printing the binomial coefficient using combination formula 
    //Number of binomial coefficient on each line
    // is equivalent to the current row number (i.e., the counter i)
    //n choose k or nCk = n!/(k!(n-k)!)
    
    for(unsigned int k=0;k<=n;++k){
      cout<<"   "<<n_factorial/(factorial(n-k)*factorial(k));
    }

    cout<<endl<<endl;
  }
  

  //Method 2 
 
  for(unsigned int n=0;n<row;++n){

    for(unsigned int j=1;j<(row-n);++j){
      cout<<"  ";
    }
    
    unsigned int coeff = 1;
    
    for(unsigned int k=0;k<=n;++k){
      cout<<"   "<<coeff;
      coeff = coeff*(n-k)/(k+1);
    }
  
    cout<<endl<<endl;
  }
 return 0;

}

Output:


Enter number of rows: 5
Method 1:
           1

         1   1

       1   2   1

     1   3   3   1

   1   4   6   4   1

Method 2:
           1

         1   1

       1   2   1

     1   3   3   1

   1   4   6   4   1


Comments

Popular posts from this blog

RLE Encoding and Decoding in C++

Given an input string, write a regular recursive function that returns the decoded (uncompresseed form) Run Length Encoded   (is a simple form of data compression where repeated character are replaced by count followed by the character repeated) string for the input string. Below are some examples: decode("*") =>* decode("3+") =>+++ decode("11*") =>*********** decode("101+") =>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ decode("abcde10+10*10+10x") =>abcde++++++++++**********++++++++++xxxxxxxxxx decode("\\") =>\ decode("\1\2\3") =>123 decode("13\7x") =>7777777777777x decode("5\\") =>\\\\\ decode("4\12\23\3") =>111122333 decode("4\\2\3") =>\\\\33 NOTE: To represent a single backslash, it’s necessary to place double backslashes (\\) in the input string to obtain the desired in

Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree. Example 1: Input: Tree 1 Tree 2 1 2 / \ / \ 3 2 1 3 / \ \ 5 4 7    Output: Merged tree: 3 / \ 4 5 / \ \ 5 4 7   Note: The merging process must start from the root nodes of both trees.  Source: LeetCode Solution: We traverse both trees in a preorder fashion. In

Invert Binary Search Tree

Invert a binary tree. Example: 4 / \ 2 7 / \ / \ 1 3 6 9   to   4 / \ 7 2 / \ / \ 9 6 3 1  4 / \ 2 7 / \ / \ 1 3 6 9   Trees are crucial data structure in Computer Science. Often trees related problems are solved using recursion as it provides an elegant solution in this particular problem at least. The basic terminology to solve this problem is to swap the left and right values, if you work on the tree from the bottom the to the top swapping the left and right node, the tree will be inverted. Let's go through an example: You can start from left or right. So, starting from the left we have   2 2 (swap the left and right node) / \ => / \ 1 3 3 1   7 7 (similarly, on the right sub-tree) / \ => / \ 6 9 9 6    Which gives,   4 / \ 2 7 / \ / \ 3 1 9 6 And finally, 4 (swap ag