Hello I'm a newbie java student, I would like to ask how can I get this kind of program output by using java:

10 10 10 10 10 10 10 10 10 10
9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

and this:

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

pls help me coding this.. any code using while, for, else or if.. thx for those who can help me.. thanks in advance..

Recommended Answers

All 2 Replies

I'm not gone write code for this as it is to simple, I will give you pseudo code and you work it out

1) initialize variable to some value
2) run a loop (for loop or while) to print that variable so many times as it its value
3) decrement by one
4) repeat previous 3 step until you hit zero

Second program, similar approach but this time use loop inside loop. Main loop to count lines, inside loop to populate content of the line.

If you have problems post your code, and we may have look at it

1)

public class Test {
       public static void main(String[] args) {
            for (int i=10;i>0;i--) {
            for(int j=0;j<i;j++) {
                System.out.print(i+" ");
            }
            System.out.println("");
        }
    }
}

output:

10 10 10 10 10 10 10 10 10 10 
9 9 9 9 9 9 9 9 9 
8 8 8 8 8 8 8 8 
7 7 7 7 7 7 7 
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1 

2)

public class Test {
    public static void main(String[] args) {
        for (int i=1;i<=5;i++) {
            for(int j = i; j<=5; j++) {
                System.out.print(j +" ");
            }
            System.out.println("");
        }

    }
}

Output:

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
commented: I did not provide solution so you can come with something useless -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.