Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help with printing right angle triangle
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Sleeping Giant




PostPosted: Fri Apr 12, 2013 10:45 am   Post subject: Need help with printing right angle triangle

This is the code I came up with

public class TriangleNumber3 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int size = 8;
for (int row = 1;row <= size; row++){
for(int col = 1;col <= row;col++){
System.out.print(col);
}
System.out.println();
}

}
}
and the result is this

1
12
123
1234
12345
123456
1234567
12345678

but the result that I want for the exercise is this

1
21
321
4321
54321
654321
754321
87654321

any ideas on how I can modify the code. Have to use nested for loops for the exercise.

Thanks
Sponsor
Sponsor
Sponsor
sponsor
Panphobia




PostPosted: Fri Apr 12, 2013 11:04 am   Post subject: RE:Need help with printing right angle triangle

Try making the inner loop increment down instead of up, so make the inner loop start at row, and then instead of adding 1 to it just subtract 1.
Sleeping Giant




PostPosted: Fri Apr 12, 2013 6:10 pm   Post subject: Re: Need help with printing right angle triangle

Is this what you where thinking.

// TODO Auto-generated method stub
int size = 8;
for (int row = 1;row <= size; row++){
for(int col = row;col <= row;col--){
System.out.print(col);
}
System.out.println();
}

This is what it gave me.

10 -1 -2 -3 -4 -5 -6 .........
Insectoid




PostPosted: Fri Apr 12, 2013 7:22 pm   Post subject: RE:Need help with printing right angle triangle

code:
for(int col = row;col <= row;col--)


When will this for loop exit?
Panphobia




PostPosted: Fri Apr 12, 2013 9:24 pm   Post subject: RE:Need help with printing right angle triangle

After
code:
int col = row;(this is condition for repetition);col--
when it is you are counting up you want your condition for repetition to be loop while col <= row, but if you are counting down you want something else
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: