Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to sort digits of a number in ascending order

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 23 Feb, 2021
Improve Article
Save Article

Given an integer N, the task is to sort the digits in ascending order. Print the new number obtained after excluding leading zeroes.


Input: N = 193202042
Output: 1222349
Sorting all digits of the given number generates 001222349.
Final number obtained after removal of leading 0s is 1222349.

Input: N = 78291342023

Approach: Follow the steps below to solve the problem:

Below is the implementation of the above approach:


# Python program to
# implement the above approach
# Function to sort the digits
# present in the number n
def getSortedNumber(n):
    # Convert to equivalent string
    number = str(n)
    # Sort the string
    number = ''.join(sorted(number))
    # Convert to equivalent integer
    number = int(number)
    # Return the integer
    return number
# Driver Code
n = 193202042



Time Complexity: O(N*log(N))
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!