Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have below question:
Use nested loops that display following patterns
  *
 ***
*****
 ***
  *


What I have tried:

for (int i = 0; i < 5; i++) {
    for (int j = 0; j <= i * 2; j++) {
         System.out.print("*");
	}
         System.out.println();
	}
Posted
Updated 15-Jan-18 10:26am

Java
public class Diamond
{
  public static void main( String args[] )
  {
    int N = Integer.parseInt(args[0]);
    int row, col;
    for (row=-N; row<=N; ++row)
    {
      for (col=-N; col<=N; ++col)
      {
        char c = Math.abs(col) + Math.abs(row) <= N ? '*' : ' ';
        System.out.print(c);
      }
      System.out.println();
    }
  }
}
 
Share this answer
 
Comments
wseng 19-Jan-18 0:25am    
thanks for the answer, it is clean and short !
CPallini 20-Jan-18 4:58am    
You are welcome.
If you can't handle the diamond at once, think of it as 2 triangles back to back.
your code is missing the spaces at beginning of lines.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

[Update]
Interesting link:
stanford.edu: Learn to Program[^]
 
Share this answer
 
v2
Comments
wseng 15-Jan-18 9:34am    
It is not homework, I self learning and finding exercises to do.
Patrice T 15-Jan-18 9:36am    
it is homework given by you.
pay attention to the first 2 lines in my answer.
wseng 15-Jan-18 9:35am    
Thanks for your reply, but I don't think this should be wrote as answer
Patrice T 15-Jan-18 9:41am    
your code gives you a triangle.
First line gives you a trick to make it a pyramid.
second line tells you what is missing to make it a diamond.
wseng 15-Jan-18 9:43am    
just wonder how to make decrements for the fourth and fifth rows.
This is the answer
for (int i = 1; i < 5; i += 2) {
			for (int j = 0; j < 4 - i / 2; j++)
				System.out.print(" ");

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

			System.out.println();
		}

		for (int i = 5; i > 0; i -= 2) {
			for (int j = 0; j < 4 - i / 2; j++)
				System.out.print(" ");

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

			System.out.println();
		}
 
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