Using a for loop to print a pattern in Java

advertisements

I am trying to print out this pattern using a for loop in Java but I am kind of stuck.

zzzzz
azzzz
aazzz
aaazz
aaaaz
aaaaa

I can print:

 a
 aa
 aaa
 aaaa
 aaaaa

using:

String i = " ";
int a = 0;
for (i="a";i.length()<=5;i=i+"a")
    System.out.println(i);

and

 zzzzz
 zzzz
 zzz
 zz
 z

using:

String i = " ";
for (i="zzzzz";i.length()>0;i=i.substring(0,i.length()-1))
    System.out.println(i);

But I can't figure out how to combine them. I was thinking about replacing the substring of i and increasing the value of the end index by one everytime but not sure of to code it. I started with something like this:

String i = " ";
String b = " ";
for (i="zzzzz";i="aaaaa";i=i.replace(i.substring(0,))
    System.out.println(i);

Any ideas?


Pseudocode :

for(i <- 0 to 5) {
  print( i times "a" followed by (5 - i) times "z")
  print a new line
}

Now implement this in Java.