Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I had tried solving the problem but was stuck here.
Hope to get some help here.

Given the number of rows, n, as input, write a program to print the following pattern.


example:
input: 4(number of rows for the pattern)

expected output:
       1
      1 2
     1   3
    1     4
     1   3 
      1 2
       1


What I have tried:

public static void main(String[] args) {
   int n = Integer.parseInt(args[0]);

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

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

     if(i!=1)
     System.out.print(i+1);
     System.out.println();
   }
 }
Posted
Updated 25-Sep-21 3:12am
Comments
Patrice T 25-Sep-21 7:06am    
And you have a problem or question ?
OriginalGriff 25-Sep-21 9:13am    
"This code I found doesn't do the whole job, can you finish my homework for me?" I'm guessing ...

1 solution

If I run your code, it's pretty obvious that all you need to do is run the same code again, but in descending order to complete the bottom of the diamond.

You wrote that code, so it should be trivial for you to complete it!
If you didn't, then you need to understand how it works in order to complete it - which shouldn't take more than a minute or so, it's not exactly complicated!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900