Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Creating a pyramid  RSS feed

 
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can anyone help me with this? I need to make a pyramid that displays these numbers but I have no idea how to do this. I end up getting different numbers besides the ones I need.
                         1                            
                                             1   2   1                        
                                        1   2   4   2   1                    
                                    1   2   4   8   4   2   1              
                               1   2   4   8  16   8   4   2   1        
                           1   2   4   8  16  32  16   8   4   2   1    
                      1   2   4   8  16  32  64  32  16   8   4   2   1  
                1   2   4   8  16  32  64 128  64  32  16   8   4   2   1

This is the code I'm working with:

 
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 I end up getting different numbers besides the ones I need.


Please copy the program's output and paste it here to show what you are talking about.
 
Andrea Menjivar
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what it currently looks like
Screenshot-(93).png
[Thumbnail for Screenshot-(93).png]
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that looks like a good shape.  Now you need to change what numbers are printed in the shape.  
What is the relationship between the current numbers that are printed and the desired numbers?
 
Bartender
Posts: 700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your second loop, you should be printing the  (j-1)th power of 2, not j. Same with third loop.
But you should change the number of spaces printed in the first loop depending on the number of row and therefore the number of digits in the largest value, so that the pyramid will look symmetrical.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!