0

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..

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by sameerrathore
0

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

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

Edited by mike_2000_17: Fixed formatting

Votes + Comments
I did not provide solution so you can come with something useless
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.