Tommy
Tommy

Reputation: 31

How to print this pyramid pattern?

I am new to java. I have no idea how to print this pyramid pattern...

This is my attempt:

for (int i=0;i<=input;i++) {
   for (int j=1;j<=i;j++) {
       System.out.print("x");
   }
   System.out.println();
}

The expected output when the input = 8:

       x
      xox
     xoxox
    xoxoxox
   xoxoxoxox
  xoxoxoxoxox
 xoxoxoxoxoxox
xoxoxoxoxoxoxox

Upvotes: 2

Views: 1591

Answers (5)

Akash Vishwakarma
Akash Vishwakarma

Reputation: 177

Without using any loop JavaScript

    var subArray = (space, star, temp = '') => {
    if (space + star <= 0) { return (temp); }
        var arr = (temp.length < space+temp.length) ? ' ' :  '*';
        arr == '*' ? star = star-1 : space=space-1;
        return subArray(space, star, (temp + arr));
}

var pascal = (size, sta = 1, x = 1) => {
        if (x-1 == size) { return false }
        console.log(subArray(size-x, sta));
        pascal(size, sta+= 2, ++x);
}

pascal(10);

Upvotes: 0

Priyam Gupta
Priyam Gupta

Reputation: 250

Use a variable to persist the pattern, it helps to get rid of unnecessary loop.

    StringBuffer pattern = new StringBuffer("x");
    for(int i = 1 ; i <= 8; i++) {
        for(int j = 8 ; j > i ; j--)
            System.out.print(" ");

        // print the pattern for current iteration before appending "ox"
        System.out.print(pattern);
        pattern.append("ox");
        System.out.println("");
    }

Upvotes: 0

BJ CHOI
BJ CHOI

Reputation: 13

Line 1 : x

Line 2 : xox (# of "ox" : 1)

Line 3 : xoxox (# of "ox" : 2)

...

Line n : xoxox....ox (# of "ox : n-1)

So, the nth Line is as follows.

    public static void printXO(int n){
    if(n < 1){
        return;
    }   
    System.out.print("x");
    for(int i = 1 ; i < n ; i++){
        System.out.print("ox");
    }
    System.out.println();
}

For nth line, we need input-n spaces.

for(int n = 1 ; n <= input ; n++){
        for(int j = 0 ; j < input - n ; j++)
            System.out.print(" ");
        printXO(n);
    }

Here is the full code.

public class Pyramid {
    public static void main(String[] args) {

        int input = 8;
        for(int n = 1 ; n <= input ; n++){
            for(int j = 0 ; j < input - n ; j++)
                System.out.print(" ");
            printXO(n);
        }

    }

    public static void printXO(int n){
        if(n < 1){
            return;
        }   
        System.out.print("x");
        for(int i = 1 ; i < n ; i++){
            System.out.print("ox");
        }
        System.out.println();
    }
}

Upvotes: 0

Amit Gujarathi
Amit Gujarathi

Reputation: 1100

This method draws a pyramid pattern using asterisk character. You can * replace the asterisk with any other character to draw a pyramid of that.

 public static void drawPyramidPattern() {
     for (int i = 0; i < 5; i++) {
     for (int j = 0; j < 5 - i; j++) {
     System.out.print(" "); 
    }
     for (int k = 0; k <= i; k++) {
     System.out.print("* ");
//write your business logic here to display alternate x and 0
     }
     System.out.println(); 
    }
    }

Upvotes: 0

Sky
Sky

Reputation: 1433

You need to print space before you print the symbols. Also, you can use if (k%2 == 0) to print two kinds of symbols.

for (int i=0; i<=input; i++) {
    for(int j=input; j>=i; j--) {
        System.out.print(" ");
    }

    for (int k=1; k<=i*2-1; k++) {
        if (k%2 == 0)
            System.out.print("o");
        else
            System.out.print("x");
    }

    System.out.println();
}

Upvotes: 7

Related Questions