Answers for "how to print pascal triangle in c++"

C++
0

Pascal triangle using c++

#include <iostream>
using namespace std;


int fact(int num){
    int factorial = 1;
    for (int i = 2; i <= num; i++)
    {
        factorial = factorial * i;
    }
    return factorial;
}

int main(){
    int n;
    cout << "Enter number of rows: ";
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j <=i; j++)
        {
            cout << fact(i) / (fact(j) * fact(i - j)) << " ";
        }
        cout << endl;
    }
    
}
Posted by: Guest on September-05-2021
1

pascal triangle c++

int pascal(int row, int col) {
  if (col == 0 || col == row) return 1;
  else if(col == 1 || (col + 1) == row) return row;
  else return pascal(row - 1, col - 1) + pascal(row - 1, col);
}
Posted by: Guest on September-22-2020
-1

nested for loops pyramid c++

//C++ program to display hollow star pyramid

#include<iostream>
using namespace std;

int main()
{
   int rows, i, j, space;

   cout << "Enter number of rows: ";
   cin >> rows;

   for(i = 1; i <= rows; i++)
   {
      //for loop to put space in pyramid
      for (space = i; space < rows; space++)
         cout << " ";

      //for loop to print star
      for(j = 1; j <= (2 * rows - 1); j++)
      {
         if(i == rows || j == 1 || j == 2*i - 1)
            cout << "*";
         else
            cout << " ";
      }
      cout << "n";
   }
   return 0;
}
Posted by: Guest on November-11-2020

Code answers related to "how to print pascal triangle in c++"

Browse Popular Code Answers by Language