Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Pascaltriangle.cpp
- Copyright:
- Author: Mr.Kindle
- Date: 30-11-22 21:44
- Description: This code print the pascal triangle of given number of rows.
- youtube: https://youtu.be/tHeyKF4iv2w
- */
- #include<iostream>
- using namespace std;
- int ncr (int, int);
- int factorial(int);
- int main()
- {
- int n,i,j;
- cout << "Enter number of rows: \n";
- cin >> n;
- /*now making pascal triangle*/
- for(i=0;i<n;i++)
- { /*for printing space*/
- for(j=0;j<n-i;j++)
- printf(" ");
- /*for printing numbers*/
- /*if we condider pascal triangle as a matrix then each number printed is nCr of corresponding 'i'
- and 'j'. where 'i' is corresponding roe index and 'j' is corresponding column index*/
- for(j=0;j<=i;j++)
- printf("%d ",ncr(i,j));
- printf("\n");
- }
- return 0;
- }//main
- int ncr (int i, int j)
- {
- int num;
- num = factorial(i)/(factorial(i-j) * factorial(j));
- return num;
- }//end of ncr
- int factorial(int n)
- {
- int prod=1,i;
- for(i=1;i<=n;i++)
- prod = prod * i;
- return prod;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement