Pjoost
Pjoost

Reputation: 1

How to make a number triangle java

I need to produce a triangle as shown:

***4
**34
*234
1234

My code is this:

for(int i=3; i>0 ;i--)
    for(int j=0; j < i; j++){
        System.out.print("*");
    }
    for(int s3 = 5; s3 >= 0; s3 -- ){
        for ( int n2 = s3  + 1; n2 <= 4; n2 ++){                                             
           System.out.print(n2);
        }
        System.out.println();
    }
}

which gives me this:

***

4
34
234
1234
**

4
34
234
1234
*

4
34
234
1234

Can anybody help me with this?

Upvotes: 0

Views: 887

Answers (6)

Yogesh Rathi
Yogesh Rathi

Reputation: 6499

Try This

for(int i=4; i>0; i--){
    for(int j=i-1; j>0; j--){
        System.out.print("*");
    }
    System.out.print(i);
    for(int k=i+1; k<=4; k++){
        System.out.print(k);
    }
    System.out.println();
}

Upvotes: 0

R&#233;my  Baron
R&#233;my Baron

Reputation: 1399

you can try this:

    int valeur=5;
    for(int i=valeur; i>0 ;i--) {
        for (int j=1;j<=valeur;j++) {
            System.out.print(j<i?"*":(j+""));
        }
        System.out.println("");
    }

Upvotes: 0

QuakeCore
QuakeCore

Reputation: 1926

actually it just occurred to me why do we even use nested loops to solve problems like this especially when drawing something like

*
**
***
****

why cant we just build Strings as we go something like:

    StringBuilder stars = new StringBuilder("****");
    for (int i = 3; i >= 0; --i) {
        stars.setCharAt(i, (char) (i + 49));
        System.out.println(stars);
    }

btw the code above will only work if you 9 or less stars, but I am just opening your mind to new ideas that you could use and practice :)

Upvotes: 0

Henri Benoit
Henri Benoit

Reputation: 725

Just do it like this:

        for(int i=3; i>=0 ;i--) {
            for(int j=0; j < i; j++) {
                System.out.print("*");
            }
            for ( int k = i+1; k < 5; k++ ) {
                System.out.print(k);
            }
            System.out.println();
        }

You just need an outer loop and 2 inner loops. The outer loops counts back from 3 to 0 (the number of stars for this line). The first inner loop prints that many stars. The second one fills the rest with digits.

Note that whether you start from 3 to 0 or 4 to 1 and then use a +1 or not and a -1 or not doesn't really matter. Also whether the first loop count forwards or backwards doesn't matter.

I just started with 3 because I find it easier to understand if i is the number of stars in the line. And I count forward (from 0 to i-1) in the first loop, just because I myself find it more intuitive to count in this direction than in the reverse one.

Upvotes: 2

mvd
mvd

Reputation: 2720

final int NUM = 4;
for (int i = NUM; i >= 1; i--) {
    for (int star = 1; star < i; star++) {
        System.out.print("*");
    }
    for (int j = i; j <= NUM; j++) {
        System.out.print(j);
    }
    System.out.println();
}

Upvotes: 0

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

Achieve it by using two levels of nested loop.

for(int i=4;i>0;i--){
     for(int j=i-1;j>0;j--){
         System.out.print("*");
     }
     System.out.print(i);
     for(int k =i+1;k<=4;k++){
         System.out.print(k);
     }
     System.out.println();
 }

Upvotes: 0

Related Questions