• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pyramid of Numbers  RSS feed

 
Iman Maulana
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:Hi,

I am trying below code to print pyramid of numbers


i got output as below


Please enter number:
123456
  1
  22
  333
  4444
  55555
  666666



i want instead output something like pyramid as below with proper spaces at the beginning and at the end when i enter any given number like 123456 or 123 or 786 and also i need those numbers to be sorted alos



Please enter number:
123456
       1
      22
     333
    4444
  55555
666666

how to tweak my code to get pyramid of sorted numbers as above?
please advise
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have split your post off the old thread because I thought it merits a thread of its own.

So, you can print a triangle of numbers aligned left, but want to insert space on the left? Can you work out how many spaces to insert? Let's say the largest number is 6; how many spaces would go to the left of 3? Is there anything you need to add to what was posted in the old thread?
Maybe you shouldn't copy the technique of using Scanner#nextLine. I am sure there are better ways to get numbers out of the command line.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!