0% found this document useful (0 votes)
11 views3 pages

C++ Loop Patterns and Outputs

programing language cpp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

C++ Loop Patterns and Outputs

programing language cpp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <iostream>

using namespace std;

int main() {
// Outer loop for rows
for (int i = 1; i <= 5; i++) {
// Inner loop for columns
for (int j = 1; j <= 5; j++) {
cout << i * j << "\t"; // Print the product of i and j
}
cout << endl; // Move to the next line after each row
}
return 0;
}
Output.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

#include <iostream>
using namespace std;

int main() {
// Number of rows
int rows = 5;

// Outer loop for rows


for (int i = 1; i <= rows; i++) {
// Inner loop for printing stars
for (int j = 1; j <= i; j++) {
cout << "* "; // Print star
}
cout << endl; // Move to the next line after each row
}
return 0;
}

*
**
***
****
*****
#include <iostream>
using namespace std;

int main() {
int rows = 3, columns = 3;
int value = 1;

// Outer loop for rows


for (int i = 0; i < rows; i++) {
// Inner loop for columns
for (int j = 0; j < columns; j++) {
cout << value++ << "\t"; // Print value and increment it
}
cout << endl; // Move to the next line after each row
}
return 0;
}

1 2 3
4 5 6
7 8 9

#include <iostream>
using namespace std;

int main() {
int rows = 5;

// Outer loop for rows


for (int i = rows; i >= 1; i--) {
// Inner loop for printing numbers
for (int j = 1; j <= i; j++) {
cout << j << " "; // Print the number
}
cout << endl; // Move to the next line after each row
}
return 0;
}

12345
1234
123
12
1

#include <iostream>
using namespace std;

int main() {
int rows = 3; // Number of rows in the pyramid

// Outer loop for each row


for (int i = 1; i <= rows; i++) {
// Inner loop for printing spaces
for (int j = i; j < rows; j++) {
cout << " "; // Print spaces to center the stars
}

// Inner loop for printing stars


for (int k = 1; k <= (2 * i - 1); k++) {
cout << "*"; // Print stars
}

// Move to the next line after each row


cout << endl;
}
return 0;
}

*
***
*****

You might also like