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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Tried to print diamond shape in java, but struggling!!  RSS feed

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Report post to moderator
posted 30 April 2008 15:27
--------------------------------------------------------------------------------
I am trying to print diamond in java, but struggling a lot to get it print.

code:

public class diamond
{

public static void main ( String args[] )
{

for( int i=1;i<=4 ; i++)
{

for(int k=1; k<=i; k++)
System.out.print("*");

System.out.println();
}
//System.out.println();



for( int i=4;i>=1 ; i--)
{
//for(int j =i-1; j>=0 ;j--)
for(int k=i-1; k>=1; k--)
System.out.print("*");
System.out.println();

}




}

}
output:
**
***
****
***
**
*

I think there si sapce issue which is not getting me to print. can you guys help me please
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Report post to moderator
Nested loops are tedious to get right, just take a diversionary stroll and re-work it as many times as it takes to get it to work. Also, study recursion, stacks, parsing and nested loops.

 
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't ask the same question twice.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!