aspose file tools*
The moose likes Beginning Java and the fly likes How can i print a diamond Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can i print a diamond" Watch "How can i print a diamond" New topic
Author

How can i print a diamond

surya sadhu
Greenhorn

Joined: Feb 16, 2004
Posts: 13
Hi guys,I want to write a program which prints '*' in a diamond shape.
thanks
[This message has been edited by surya (edited July 23, 2000).]
[This message has been edited by surya (edited July 23, 2000).]
Eric Edwards
Ranch Hand

Joined: Feb 12, 2000
Posts: 60
Originally posted by surya:
Hi guys,I want to write a program which prints '*' in a diamond shape.
thanks
[This message has been edited by surya (edited July 23, 2000).]
[This message has been edited by surya (edited July 23, 2000).]
[/I would set it up using a "for loop", possibly a nested "for loop".]
cathy joaqs
Greenhorn

Joined: Nov 14, 2004
Posts: 2
elow cn u share me the program which prints '*' in a diamond shape using loops in JAVA
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Most people around these parts are unwilling to do homework problems for others. The whole point of the assignment is so you can learn how to write a basic Java program.

With that said, we ARE willing to help if you have specific questions or run into problems along the way. Please post more information about what you have tried so far. This program should be short enough that you can post your actual code. Also, if you have compiler errors, post those, along with some indication of which lines in the code corresponds to the errors.

Good luck with learning Java!

Layne


Java API Documentation
The Java Tutorial
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
You could create 2 routines. 1 prints a '*' and the other prints a space.
On the first line of printing you call the space printing several times and the '*' printing only once.
Building toward the center of the diamond, on each line you need the '*' routine more often and the space printing routine less often.
In the midddle of the diamond you call only the '*' printing routine.

Can you do it?
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Think of what is possible. If you are writing to the command line then you will not be able to get a away with the simplest of diamonds i.e 1,2,1 because it will look like the following:
*
**
*

So you will need to produce a diamond of spaces and *'s which looks like

So by looking at the above we can see the height and width needed in the for loop(s). By looking at the above we can also see that as long as the middle line contains an odd number greater or equal to 3 *'s we can quite easily produce a diamond.
[ November 15, 2004: Message edited by: Nigel Browne ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can i print a diamond