Reputation: 33
How can I print the following pattern with the for statement?
AAAA
AAAB
AABB
ABBB
BBBB
What I tried to do:
Code:
int stars = 4;
for (int row = stars; row >= 1; row--)
{
for (int i = 0; i < row; i++)
{
Console.Write("A");
}
Console.WriteLine();
}
Upvotes: 1
Views: 55
Reputation: 13222
You where almost there.
>= 1
to >= 0
). We need 5 rows for 4 stars, 6 rows for 5 stars, etc.stars
is 4).A
or B
based on the iteration number of both loops.See code below:
int stars = 4;
for(int i = stars; i >= 0; i--) {
for(int j = 0; j < stars; j++) {
if(i > j) {
Console.Write('A');
}
else {
Console.Write('B');
}
}
Console.WriteLine();
}
Upvotes: 3