solve this problem by c++ program
Attachments:
Answers
Answered by
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
Computer Science,
1 month ago
Science,
1 month ago
Social Sciences,
1 month ago
Math,
2 months ago
Physics,
2 months ago
Science,
9 months ago
Chemistry,
9 months ago