0

below are my code can anyone help me i stuck at the * part

i want the output like this

     9
    989
   98789
  9876789
 987656789

... and so on

    for (int i = 1; i <= l; i++) {
        for (int j = 1; j <= l - i; j++)
            System.out.print(" ");

        for (int j = 1; j <= (2 * i - 1); j++) {
            if((l-j) < l-i){
            // here System.out.print("*");
            }
            else
                System.out.print(l - j);

        }
        System.out.print("\n");
    }
3
  • What have you tried? What isn't working? Are we supposed to run your code to determine what's wrong? Commented Dec 15, 2010 at 15:04
  • 2
    @org Nah, I often find the need at work to output triangles of numbers :) Commented Dec 15, 2010 at 15:04
  • @Falmarri I'm guessing he's tried the code snippet he posted, and he's having problems on the line commented // here ... Commented Dec 15, 2010 at 15:06

3 Answers 3

1

I ended up rewriting it; I couldn't figure out a concise constant for the print you were missing. If you loop i from 0 to l (inclusive), you can do:

Spaces

The last line (i = l) has 0 spaces, so each line has l-i spaces:

for(int j = 0; j < l-i; j++)
    System.out.print(" ");

Left/Center

Every line starts at l, and you want to count down till you reach the center; since the current line has l-i spaces and the center is at l+1 (since I included the line with a 0 center), which is i+1 away, you need to output through l-i inclusive

for(int j = l; j >= l-i; j--)
    System.out.print(j);

Right

The same loop as above, but reversed. Starts 1 higher because the center is already handled above and you don't want to output it again

for(int j = l-i+1; j <= l; j++)
    System.out.print(j);
Sign up to request clarification or add additional context in comments.

Comments

0

I'd rework a little more than just that one line

    for (int j = 1; j <= (2 * i - 1); j++) {
        if((l-j) < l-i){
        // here System.out.print("*");
        }
        else
            System.out.print(l - j);

To...

for(int j = 1; j < i; j++)
     System.out.print(10-j);

System.out.println(10-i);

for(int j = 1; j < i; j++)
     System.out.print(10-i+j);    

Comments

0

This is completed program. I used "divide and conquer" method (basically the same idea as of Michael Mrozek), and use method to make it extreme-easy-to-understand program . Hope this will help.

public class PatternPrinter {
public static void main(String[] args) {
    printAllRows(9, 5);
}

public static void printAllRows(int max, int numOfRows) {
    for (int i = 1; i <= numOfRows; i++) {
        printRow(numOfRows - i, max, i);
        System.out.print("\n");
    }
}

public static void printRow(int numOfSpaces, int max, int num) {
    printSpaces(numOfSpaces);
    printLeftAndCenterPart(max, num);
    printRightPart(max, num - 1);
}

public static void printRightPart(int max, int num) {
    while(num > 0) {
        System.out.print(max - num + 1);
        num--;
    }
}

public static void printLeftAndCenterPart(int max, int num) {
    while(num > 0) {
        System.out.print(max--);
        num--;
    }
}

public static void printSpaces(int numOfSpaces) {
    while(numOfSpaces > 0) {
        System.out.print(" ");
        numOfSpaces--;
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.