Win a copy of Python Continuous Integration and Delivery this week in the Python 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

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: 678
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.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!