Computer Science, asked by buttfiza66, 2 months ago

solve this problem by c++ program

Attachments:

Answers

Answered by anindyaadhikari13
2

Answer:

This is the required C++ program for the question.

#include <iostream>

using namespace std;

int main()

{

   int i,j,n=4;

   for(i=1;i<=n;i++)   {

       for(j=1;j<i;j++)

           cout << "  ";

       for(j=1;j<=n-i+1;j++)

           cout <<"* ";

       cout << endl;

   }

   return 0;

}

Explanation:

  • Patterns are solved using nested loop. Outer loop - Displays the rows.Inner loop - Displays the columns.
  • As there are 4 rows, outer loop iterates 4 times.In each row, there are 2(i - 1) spaces where i is the row number, so, the first inner loop iterates i - 1 times and displays 2 spaces after each iteration.
  • Now, in each row, there are n - i + 1 stars (i = row number, n = total number of rows). So, second inner loop iterates n - i + 1 times and displays a star with space.
  • After displaying the cols, a new line is inserted.

See the attachment for output.

Attachments:
Similar questions