Here, we’ll learn how to draw Pascal’s triangle using C programming.

The Pascal’s triangle is as given below:

1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

Algorithm:

  1. To print the Pascal’s triangle we will use three loops which are two for loops and one while loop.
  2. The first loop is used to print the number of rows.
  3. The second loop is used to print the spaces between the stars.
  4. The third loop which is while loop is used to print the stars.

Code: 

#include <iostream>
using namespace std;
int main()
{
   int n, k = 0;
   cout<<"Enter number of rows: ";
   cin>> n;
   cout<<"\n";
   for (int i = 1; i <= n; ++i)
   {
      for (int j = 1; j <= n - i; ++j)
      	cout<<"  ";
      k=0;
      while (k != 2 * i - 1)
      {
			cout<<"* ";
			++k;
      }
      cout<<"\n";
   }
   return 0;
}

Output:

Enter the number of rows: 5

        *

      * * *

    * * * * *

  * * * * * * *

* * * * * * * * *

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019