Results 1 to 10 of 19
-
10th Sep 2011, 04:51 AM #1OPBanned
[JAVA} Need help in a Nested Loop
Any1 can help me in java that can will have an output like these please..
Code:54321 4321 321 21 1
conrey Reviewed by conrey on . [JAVA} Need help in a Nested Loop Any1 can help me in java that can will have an output like these please.. 54321 4321 321 21 1 Rating: 5
-
10th Sep 2011, 04:55 AM #2Member
123456
12345
1234
123
12
1
public class PatternExample{
public static void main(String[] args){
for(int i=6;i>=1;i--){
for(int j=1;j<i+1;j++){
System.out.print(j);
}
System.out.println();
}
}
}
source :
http://www.roseindia.net/answers/vie...-Question.html
-
10th Sep 2011, 06:36 AM #3Teh GFX Whore! ^.^
Though busylife already did it but if you want the code to generate exact same output which you wanted, then here it is:
class nitish{
public static void main (String args[]) {
for (int i = 5; i >=0; i--) {
for (int j = i; j > 0; j--) {
System.out.print(j);
}
System.out.println();
}
}
}
-
10th Sep 2011, 08:09 AM #4OPBanned
-
10th Sep 2011, 08:21 AM #5Teh GFX Whore! ^.^
Your most welcome. If you need any further java help, then you can directly PM me
-
10th Sep 2011, 08:46 AM #6OPBanned
sure I'll always remember it bro...
if its ok can you please xplain how does the code really works? I mean the difference of the outer for loop and the inner for loop?
-
10th Sep 2011, 08:59 AM #7Teh GFX Whore! ^.^
or (int i = 5; i >=0; i--) {
for (int j = i; j > 0; j--) {
System.out.print(j);
}
System.out.println();
}
This is your code.
You have 2 variables, i and j.
Try placing yourself in place of compiler. Write i and j on register.
Now its written , int i=5. That means "i" is an integer with value 5 assigned to it and if you know the syntax of "FOR" loop, second condition limits the value and third conditions proceeds the loop.
So, coming in the nested loop, where j is equal to 5.
So, next line is printing j.
So we write the value of j on the register which is equal to 5 and just before that we check the condition(j>0 ?? yes? ok, proceed)
So your register has
5 written on it.
now, j-- . so j is 4. check again, is j>0? yes? ok, proceed,
54
now, j=3. check again..
543
now j =2, check again, ..
5432..
now, j=1, check agian..
54321
now j=0 , check, no? come out of the loop.
println() means taking the cursor to next line.
now i--.
so i=4.
and j =i therefore j=4.
and again start with printing j and checking..
so it will be
54321
4
then 54321
43... and so on. this is how program will work in very lay man's terms.
-
10th Sep 2011, 09:14 AM #8MemberWebsite's:
imdber.org justpaste.meCode:public class apathetic{ public static void main(String[] args){ for(int a=5;a>0;a--){ for(int b=a;b>0;b--){ System.out.print(b); } System.out.println(); } } }
-
10th Sep 2011, 09:31 AM #9OPBanned
-
10th Sep 2011, 01:12 PM #10Respected Member
The inner loop causes the numbers to print without the line break so it starts with highest to lowest.
The outer loop subtracts 1 from the highest and causes a line break.
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Java stinks: Turn off your Java browser plugin
By Loget in forum General DiscussionReplies: 37Last Post: 5th Sep 2012, 07:40 AM -
[PHP] Need help to loop true the results
By AndroidApps in forum Web Development AreaReplies: 6Last Post: 5th Feb 2012, 09:56 AM -
Php Loop Prob
By Chris2k in forum Web Development AreaReplies: 1Last Post: 16th Jan 2012, 02:00 PM -
What's the biggest forum out there? Been out of the loop..
By wildfire95 in forum Webmasters, Money MakingReplies: 3Last Post: 7th May 2011, 11:28 AM -
Redirect loop?!?!
By GeeZus in forum Technical Help Desk SupportReplies: 4Last Post: 9th Jun 2009, 04:36 AM
themaManager - edit and manage...
Version 4.17 released. Open older version (or...