• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can i print a diamond

 
surya sadhu
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
elow cn u share me the program which prints '*' in a diamond shape using loops in JAVA
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic