Reputation: 75
This is the nested for loop I have written:
int i,j;
for(i=5;i>=1;i=i-1)
{
for(j=1;j<i+1;j++)
{
System.out.print(i);
}
}
the above code prints: 555554444333221 but I'm trying to get it to add another '2' on the end, so it should print 5555544443332212.
I've spent a while changing the operators and the numbers but I haven't managed to figure it out yet.
Upvotes: 1
Views: 569
Reputation: 20751
simply just print a 2 at the end of for loop
for(i=5;i>=1;i=i-1) {
for(j=1;j<i+1;j++)
{
System.out.print(i);
}
}
System.out.print(2);
Upvotes: 1
Reputation: 3302
Just add System.out.print(2)
after the outer loop:
for(i=5;i>=1;i=i-1) {
for(j=1;j<i+1;j++)
{
System.out.print(i);
}
}
System.out.print(2);
As a side note, you can define i
and j
inside the for
definitions:
for(int i=5;i>=1;i=i-1) {
for(int j=1;j<i+1;j++) {
Upvotes: 4