Pascal's Triangle

#include "stdafx.h"
#include "iostream"
using namespace std;
int main() // main function declared
{

        cout<<"\n\t\t\t PASCAL'S TRIANGLE \n\n";
        int rows;
        cout << "Enter the number of rows : "; // Take no of rows as input from user
        cin >> rows;
        cout << endl;

          for (int i = 0; i < rows; i++)     //outer loop for number of rows
                {
                    int val = 1;
                    for (int j = 1; j < (rows - i); j++) // loop for spaces before the triangle
                    {
                            cout << "    ";
                    }
                    for (int k = 0; k <= i; k++) // loop to print coefficients and maintain space btw them
                    {
                            cout << "       " << val;
                            val = val * (i - k) / (k + 1); // calculating coefficients
                    }
                            cout << endl << endl;
                }
                cout << endl;
                           system("pause");
                return 0; // for getting output
}







Share on Google Plus