1

I am struggling to build an algorithm that would print the much needed pattern. The code is the following:

public static void printPatternH(int size)
{
    for (int row = 1; row <= size; row++)
    {
        for (int col = 1; col <= 2*size; col++)
        {
            if (col > size + row - 1) {
                continue;
            }
            if (col <= size) {
                System.out.print((row + col >= size + 1 ? (row + col)%size : " ") + " ");
            }
            else {
                System.out.print((row + col >= size + 1 ? (row + size)%col : " ") + " ");
            }                
        }
        System.out.println();
    }
}

The result is:
enter image description here

I understand that if size is 9 the last number in the middle will be 0 as (row + size)%col = 0, however I couldn't figure out a way to modify it without changing the rest of the values.

4
  • so you want to print "9" instead of "0" ? Commented May 9, 2018 at 8:47
  • 3
    (row + col)%size -> (row + col - 1) % size + 1 Commented May 9, 2018 at 8:51
  • @saka1029, this can be the answer. care to post it :) Commented May 9, 2018 at 8:52
  • @AgelKoh yes, exactly, the solution from saka1029 fulfilled my query :) thanks Commented May 9, 2018 at 9:34

4 Answers 4

3

Change

(row + col)%size

to

(row + col - 1) % size + 1
Sign up to request clarification or add additional context in comments.

Comments

1

You can check for the "0" and replace it before printing it out:

 if (col <= size) {
     //print left hand side
     int remainder =  (row + col) % size;
     if (remainder == 0) remainder = size; //replace the "0" with size here.
     System.out.print((row + col >= size + 1 ? remainder : " ") + " ");
} else {
     //print right hand side
     System.out.print((row + col >= size + 1 ? (row + size) % col : " ") + " ");
}

It will give this output:

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

Comments

0
public class Testb 
{
    int item = 9;
    int limit = 5;
    int half = item/2;
    public static void main(String[] args) 
    {
        //System.out.println("Hello World!");
        for(int i=0;i<limit;i++){
          
           for(int j=0;j<half;j++){
              
              if(j<(half-i)){
               
                 print(" ");
               
              }else{
              
                 print(j-(half-i))
              
              }          
           }
           
           print(i);

           for(int k=half;k<(half+i);k++){
           
               print((half+i)-(k+1));
           
           }

           println();
        }
    }
}

output:

          0
        0 1 0
      0 1 2 1 0
    0 1 2 3 2 1 0
  0 1 2 3 4 3 2 1 0

Comments

-1
 int rowCount = 1;

    System.out.println("Here Is Your Pyramid");

    //Implementing the logic

    for (int i = noOfRows; i > 0; i--)
    {
        //Printing i*2 spaces at the beginning of each row

        for (int j = 1; j <= i*2; j++)
        {
            System.out.print(" ");
        }

        //Printing j value where j value will be from 1 to rowCount

        for (int j = 1; j <= rowCount; j++)             
        {
            System.out.print(j+" ");
        }

        //Printing j value where j value will be from rowCount-1 to 1
         
        for (int j = rowCount-1; j >= 1; j--)
        {                 
            System.out.print(j+" ");             
        }                          
         
        System.out.println();

        //Incrementing the rowCount

        rowCount++;
    }

1 Comment

He does not ask a fully way different of his one, but to fix his one, he won't learn it he just use another way.

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.