Sep 8, 2024
for loop for today's examples.n lines.n times to print numbers or stars.for (int i = 1; i <= n; i++) { // Outer loop for lines
for (int j = 1; j <= n; j++) { // Inner loop for printing
cout << j; // Print numbers
}
cout << endl; // Move to next line
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
cout << j + 1; // Print numbers up to i
}
cout << endl;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) { // Print spaces
cout << " ";
}
cout << "*"; // Print first star
if (i > 0) { // Avoid printing second star for first line
for (int j = 0; j < (2 * i - 1); j++) { // Print inner spaces
cout << " ";
}
cout << "*"; // Print second star
}
cout << endl;
}