[JAVA} Need help in a Nested Loop

Status
Not open for further replies.

conrey

Banned
Banned
36
2011
0
0
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..
 
18 comments
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();
}
}
}
 
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?
 
Last edited by a moderator:
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.
 
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 (y)
 
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??
 
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.
 
Code:
/**
 * Prints the pattern in the post above.
 * 
 * @author Gaurav
 * @version 1.0
 */
public class pattern
{
    public static void main(String[] args) {
        for(int i = 1; i <= 5; i++){
            for(int j = 1; j <=5; j++){
                if(j < i){
                    System.out.print(i);
                }
                else{
                    System.out.print(j);
                }
            }
            System.out.println();
        }
    }
}

That should do it :) .

One suggestion. Please do try doing the programs yourself first.. If you're stuck somewhere then only look out for help. Just typing in the pattern and asking for the solution gives the impression that you want us to do your homework (not saying that this might be the case). But I and many others would appreciate if you post your progress as well along with the question.
 
Last edited:
nope I'm self learning bro, and I really make one for this but the problem is I didn't really think putting if-else in a nested for loop..
 
any can help me out ive been trying so hard but failed..

Output:
54321
4321
321
21
1

But using the "while" syntax. not nested for loop. is it possible?
 
Here you go:

Code:
/**
 * Prints the above mentioned pattern
 * 
 * @author Gaurav <Gaurav@wjunction.com>
 * @version 1.0
 */
public class PatternwJ
{
    public void display() {
        int n = 5;        
        while(n >= 1){
            int k = n;
            while(k >= 1){
                System.out.print(k);
                k--;
            }
            System.out.println();
            n--;
        }
        
    }
}
 
^ Thank you so much gaurav its perfect.. well I have a new problem bro our professor said "Make a nested loop, that will prints out All Even numbers in in 1-50, and when it goes to 51-100 it prints out odd numbers"
basically when its still 1<50 it will prints out 2,4,6,8..50 and when its 51-100 it prints out 51,53,55,57,59..99..

Ive tried this so far..
Code:
public class oddeven {

    public static void main(String[] args){
    	System.out.println("Even numbers: ");
    	for(int a=1;a<=50;a++){
    		if(a%2 == 0){
    			System.out.println(a);
    		}
    	}
    	System.out.println("Odd numbers: ");
    	for(int a=51;a<=100;a++){
    		if(a%2 == 1){
    			System.out.println(a);
    		}
    	}
    }


}

but our processor says make it into a single loop now I can't do it.. please please help.
 
^^
Make 1 loop, go from 0 to 100.
In there put an if clause.
So,

Code:
if a<50:
    if a%2 == 0:
        print a
else:
    if a%2 == 1:
        print a
I think you have no idea what you're doing and just wasting time.
It can't get easier than this, and you're struggling to complete them.
So do some reading and get your head around this stuff. Start from the basics. Read your textbook and make sure you understand each word. Use a dictionary if English is not your First Language.
 
System.out.println("Odd numbers: "); for(int a=51;a<=100;a++){ if(a%2 == 1){ System.out.println(a);

You Getting Wrong Here Make it 0 instead of 1
 
He wants to print odd numbers.. so the remainder after dividing by 2 should be one.. He's correct there..

Try this:

Code:
/**
 * Prints the above mentioned pattern
 * 
 * @author Gaurav <Gaurav@wjunction.com>
 * @version 1.0
 */
public class oddeven
{
     public static void main(String[] args){
    	for(int i = 1; i <= 100; i++) {
            if(i <= 50 && i % 2 == 0) {
                System.out.println(i);
            }
            else if(i > 50 && i % 2 == 1){
                System.out.println(i);
            }
        }
    }
}
 
Status
Not open for further replies.
Back
Top