• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can i print a diamond

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".]

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
elow cn u share me the program which prints '*' in a diamond shape using loops in JAVA
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic