Program to Sort digits of a Number in Ascending Order

Python Program to Sort digits of a Number in Ascending Order

In the previous article, we have discussed Python Program to Count the Number of Odd and Even Digits
The task is to sort the digits in ascending order given a number N. Print the new number after removing the leading zeroes.

join() method in python:

The join() method in Python can be used to convert a List to a String.

Iterables such as Lists, Tuples, Strings, and others are accepted as parameters for the join() process.

Examples:

Example1:

Input:

Given Number = 4561230008

Output:

The sorted digits in ascending order of a given number 4561230008 after removal of leading zeros =
1234568

Example2:

Input:

Given Number = 34879010

Output:

The sorted digits in ascending order of a given number 34879010 after removal of leading zeros =
134789

Program to Sort digits of a Number in Ascending Order in Python

Below are the ways to get sorted digits in ascending order of a given number after the removal of leading zeros:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Convert the given number into a string number using the str() function and store it in another variable.
  • Sort the above-obtained string number using the sorted() function and store it in another variable.
  • Convert this sorted list of numbers into a string using the join function and store it in another variable.
  • Convert the above obtained sorted string to an integer using the int() function (This removes the leading zeros) and store it in another variable.
  • Print the sorted digits in ascending order of a given number after removal of leading zeros.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
numb = 4561230008
# Convert the given number into a string number using the str() function and
# store it in another variable.
strng_numbr = str(numb)
# Sort the above-obtained string number using the sorted() function and store
# it in another variable.
sortdlst_num = sorted(strng_numbr)
# Convert this sorted list of numbers into a string using the join function and
# store it in another variable.
sortd_str = ''.join(sortdlst_num)
# Convert the above obtained sorted string to an integer using the int() function
# (This removes the leading zeros) and store it in another variable.
rslt = int(sortd_str)
# Print the sorted digits in ascending order of a given number after removal of
# leading zeros.
print("The sorted digits in ascending order of a given number",
      numb, "after removal of leading zeros =")
print(rslt)

Output:

The sorted digits in ascending order of a given number 4561230008 after removal of leading zeros =
1234568

Method #2: Using For loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Convert the given number into a string number using the str() function and store it in another variable.
  • Sort the above-obtained string number using the sorted() function and store it in another variable.
  • Convert this sorted list of numbers into a string using the join function and store it in another variable.
  • Convert the above obtained sorted string to an integer using the int() function (This removes the leading zeros) and store it in another variable.
  • Print the sorted digits in ascending order of a given number after removal of leading zeros.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
numb = int(input("Enter some random number = "))
# Convert the given number into a string number using the str() function and
# store it in another variable.
strng_numbr = str(numb)
# Sort the above-obtained string number using the sorted() function and store
# it in another variable.
sortdlst_num = sorted(strng_numbr)
# Convert this sorted list of numbers into a string using the join function and
# store it in another variable.
sortd_str = ''.join(sortdlst_num)
# Convert the above obtained sorted string to an integer using the int() function
# (This removes the leading zeros) and store it in another variable.
rslt = int(sortd_str)
# Print the sorted digits in ascending order of a given number after removal of
# leading zeros.
print("The sorted digits in ascending order of a given number",
      numb, "after removal of leading zeros =")
print(rslt)

Output:

Enter some random number = 34879010
The sorted digits in ascending order of a given number 34879010 after removal of leading zeros =
134789

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.