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

The inverted Pascal’s triangle is as given below:

1 6 15 20 15 6 1

1 5 10 10 5 1

1 4 6 4 1

1 3 3 1

1 2 1

1

Algorithm:

This method is similar to what we used to print pascal’s triangle.

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

Code:

//inverted pascal's triangle
#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<<" * ";
      }
	  cout<<"\n";
	  k=0;
      for (int k = 1; k <= i ; k++) 
	  {
			cout<<" ";
	  }
      
   }
   return 0;
}

Output:

Enter number of rows: 5

* * * * *
 * * * *
  * * *
   * *
    *

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019