print pattern and space in cpp
C++ Pattern Printing with Explanation
#include <iostream>
using namespace std;
int main() {
int rows, i, j;
cout << "Enter number of rows: ";
cin >> rows;
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}
- Explanation:
- The program includes the necessary header file
iostream
for input-output operations. - The
using namespace std;
statement allows the program to use entities from the standard C++ library. - In the
main
function, we declare variablesrows
,i
, andj
to store the number of rows and loop counters. - User is prompted to enter the number of rows.
- The nested
for
loops are used to print the pattern of stars and spaces based on the input provided. - The outer loop iterates through each row, and the inner loop prints the appropriate number of stars for each row.
- After printing the stars for each row, a newline character is used to move to the next row.
- Finally, the program returns 0 to indicate successful execution.