acending pyramid of numbers

gudii9
gudii9 used Ask the Experts™
on
hi,
 let us say i give some random number like 81349 on console i should see output as by ascending order of each digit in each line how to achieve that as well

     1
    3 3
   4 4 4
 8 8 8 8
9 9 9 9 9

how to to achive this
please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
First get the number in an Int array , Sort it and provide each number to the program instead of *.

NoOfRows  = Array Count.

System.out.print("* "); --> Instead o this provide the number

You can print the pyramid using this code-

int NoOfRows = 5, k = 0;

for(int i = 1; i <= NoOfRows; ++i, k = 0) {
    for(int space = 1; space <= NoOfRows - i; ++space) {
        System.out.print("  ");
    }

    while(k != 2 * i - 1) {
        System.out.print("* ");
        ++k;
    }

    System.out.println();

Open in new window


https://www.programiz.com/java-programming/examples/pyramid-pattern
dbruntonQuid, Me Anxius Sum?  Illegitimi non carborundum.

Commented:
I'd do it this way

Turn number into an array of bytes, one digit into each byte.
You can then determine the number of bytes you have.
Use a for loop to find the lowest number and then process that.  (for 1 to maximum no of bytes)
Repeat for the next lowest number and then process that.
Repeat until finished.

Note that you may have to consider cases where you have duplicate numbers such as 232456 (the 2s are duplicated twice).
Also what happens if you have a 0 in the number?
Top Expert 2016

Commented:
You've asked this question before
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Database Expert
Awarded 2016
Top Expert 2016
Commented:
Here 's final solution for you. FULL tested code.

//'main' method must be in a class 'Rextester'.
//Compiler version 1.8.0_111

import java.util.*;
import java.lang.*;
import java.util.Arrays;

class Rextester
{  
    public static void main(String args[])
    {
        int num = 81349;  
        int[]digits = Integer.toString(num).chars().map(c -> c-='0').toArray();         
        Arrays.sort(digits);        
        int n = Integer.toString(num).length();
         int k = 2*n - 2;
        
            for (int i=0; i<n; i++)
            {

                    for (int j=0; j<k; j++)
                    {
                   
                        System.out.print(" ");
                    }
                    k = k - 1;
                    for (int j=0; j<=i; j++ )
                    {
                        System.out.print(digits[i] + " ");
                    }

                    System.out.println();            
           }
    }
}

Open in new window


Output


        1 
       3 3 
      4 4 4 
     8 8 8 8 
    9 9 9 9 9 

Open in new window


Enjoy !!
Top Expert 2016

Commented:
Here 's final solution for you. FULL tested code.
We don't give OPs full code here. Try to think why
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
We don't give OPs full code here. Try to think why

I below case I have not given OPs the full code. I asked him to add a where clause. You can see my solution was selected and solutions based on my suggestion was selected as answer.

https://www.experts-exchange.com/questions/29060511/how-to-write-a-recursive-logic-in-oracle.html?notificationFollowed=198427793#a42318350
Top Expert 2016

Commented:
Sorry - i don't understand the connection between this question and the one you linked to
We don't give OPs full code here. Try to think why

Reason being . . . it is far easier to get someone else to do it. That avoids having to learn anything yourself, which as we all know is an unneeded luxury.
Shaun VermaakSenior Consultant
Awarded 2017
Distinguished Expert 2018

Commented:
Reason being . . . it is far easier to get someone else to do it. That avoids having to learn anything yourself, which as we all know is an unneeded luxury.
We don't give OPs full code here. Try to think why
Then apply this reasoning to everyone, not just ones that resembled homework. It is up to the experts whether they want to answer these, EE policy around this has changed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial