Nested Loops – Star Patterns Assignment
Course: Introduction to Programming (BSCS)
Topic: Nested Loops – Star Patterns
Instructions: 1. Write clean and readable C++ programs for each question. 2. Use nested loops to solve the
problems. 3. Test your programs with multiple inputs to ensure correctness. 4. Submit source code files
and output screenshots.
1. Solid Square Pattern
Question: Print a solid square of stars with 5 rows and 5 columns.
Output:
*****
*****
*****
*****
*****
C++ Code:
#include <iostream>
using namespace std;
int main() {
int rows = 5;
for(int i = 1; i <= rows; i++) {
for(int j = 1; j <= rows; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
2. Solid Rectangle Pattern
Question: Print a solid rectangle of stars with 5 rows and 8 columns.
1
Output:
********
********
********
********
********
C++ Code:
#include <iostream>
using namespace std;
int main() {
int rows = 5, cols = 8;
for(int i = 1; i <= rows; i++) {
for(int j = 1; j <= cols; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
3. Solid Right-Angled Triangle
Question: Print a right-angled triangle of stars.
Output:
*
**
***
****
*****
C++ Code:
#include <iostream>
using namespace std;
2
int main() {
int rows = 5;
for(int i = 1; i <= rows; i++) {
for(int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
4. Solid Inverted Right-Angled Triangle
Question: Print an inverted right-angled triangle of stars.
Output:
*****
****
***
**
*
C++ Code:
#include <iostream>
using namespace std;
int main() {
int rows = 5;
for(int i = rows; i >= 1; i--) {
for(int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
3
5. Solid Cone (Pyramid) Pattern
Question: Print a solid pyramid (cone) of stars.
Output:
*
***
*****
*******
*********
C++ Code:
#include <iostream>
using namespace std;
int main() {
int rows = 5;
for(int i = 1; i <= rows; i++) {
for(int j = i; j < rows; j++) {
cout << " ";
}
for(int k = 1; k <= (2*i - 1); k++) {
cout << "*";
}
cout << endl;
}
return 0;
}
Notes for Students: - Use nested loops for rows and columns/stars.
- Outer loop → controls rows
- Inner loop → controls columns or stars
- Pyramid/cone requires two inner loops (spaces + stars)