Activity Stream
48,167 MEMBERS
6793 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 2 12 LastLast
Results 1 to 10 of 19
  1.     
    #1
    Banned

    Default [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
    Ive tried all my best but still my code gives like infinite loop or something..
    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

  2.   Sponsored Links

  3.     
    #2
    Member
    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

  4.     
    #3
    Teh 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();
    }
    }
    }


    I joined KWWHunction before it was cool!
    My "AWESOME" Graphic designing Service


  5.     
    #4
    Banned
    Quote Originally Posted by busylife007 View Post
    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
    sorry mate I don't mean that output..but thanks anyways..

    @Nitish

    thank you so much bro.

  6.     
    #5
    Teh GFX Whore! ^.^
    Your most welcome. If you need any further java help, then you can directly PM me


    I joined KWWHunction before it was cool!
    My "AWESOME" Graphic designing Service


  7.     
    #6
    Banned
    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?

  8.     
    #7
    Teh 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.


    I joined KWWHunction before it was cool!
    My "AWESOME" Graphic designing Service


  9.     
    #8
    Member
    Website's:
    imdber.org justpaste.me
    Code: 
    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();
    		}
    	}
    }
    When doing nested loops, create the inner loop first then go for the outer loop

  10.     
    #9
    Banned
    Quote Originally Posted by Nitish View Post
    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.
    thank you so much bro..

    @apathetic

    can you please tell me why is it good to first make the inner loop first??

  11.     
    #10
    Respected 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.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Java stinks: Turn off your Java browser plugin
    By Loget in forum General Discussion
    Replies: 37
    Last Post: 5th Sep 2012, 07:40 AM
  2. [PHP] Need help to loop true the results
    By AndroidApps in forum Web Development Area
    Replies: 6
    Last Post: 5th Feb 2012, 09:56 AM
  3. Php Loop Prob
    By Chris2k in forum Web Development Area
    Replies: 1
    Last Post: 16th Jan 2012, 02:00 PM
  4. What's the biggest forum out there? Been out of the loop..
    By wildfire95 in forum Webmasters, Money Making
    Replies: 3
    Last Post: 7th May 2011, 11:28 AM
  5. Redirect loop?!?!
    By GeeZus in forum Technical Help Desk Support
    Replies: 4
    Last Post: 9th Jun 2009, 04:36 AM

Tags for this Thread

BE SOCIAL