Sort in Ascending order || for loop || Java programs || java



In this, we are going to write a simple program to re-order the digits of input number in ascending order  in Java.
Example:-
Input : 658743
Output : 345678



import java.util.*;

class Digits_ascending 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        int i, j, n, s = 0, c = 0, d;
        System.out.println("Enter a number:");
        n = sc.nextInt();
        for (i = 9; i >= 0; i--) 
        {
            for (j = n; j > 0; j /= 10) 
            {
                d = j % 10;
                if (d == i)
                {
                    s = s + d * (int) Math.pow(10, c++);
                }
            }
        }
        System.out.println("New Number:" + s);
    }
}

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post