w3resource

Python: Find the difference between the largest integer and the smallest integer which are created by 8 numbers from 0 to 9


Largest-Smallest Integer Difference

Write a Python program that determines the difference between the largest and smallest integers created by 8 numbers from 0 to 9. The number that can be rearranged shall start with 0 as in 00135668.

Input:
Data is a sequence of 8 numbers (numbers from 0 to 9).
Output:
The difference between the largest integer and the smallest integer.

Visual Presentation:

Python: Find the difference between the largest integer and the smallest integer which are created by 8 numbers from 0 to 9

Sample Solution:

Python Code:

# Print statement to prompt the user to input an integer created by 8 numbers from 0 to 9
print("Input an integer created by 8 numbers from 0 to 9.:")

# Convert the input integer to a list of characters
num = list(input())

# Print statement to calculate and display the difference between the largest and smallest integer from the given integer
print("Difference between the largest and the smallest integer from the given integer:")

# Convert the list of characters to integers after sorting in ascending and descending order
# Calculate the difference between the largest and smallest integers and print the result
print(int("".join(sorted(num, reverse=True))) - int("".join(sorted(num))))

Sample Output:

Input an integer created by 8 numbers from 0 to 9.:
 34568729
Difference between the largest and the smallest integer from the given integer:
75308643

Explanation:

Here is a breakdown of the above Python code:

  • The above code prompts the user to input an integer created by 8 numbers from 0 to 9.
  • User input is converted into a list of characters.
  • The code calculates the difference between the largest and smallest integers created from the given input.
  • Finally the result is printed.

Flowchart:

Flowchart: Python - Find the difference between the largest integer and the smallest integer which are created by 8 numbers from 0 to 9

For more Practice: Solve these Related Problems:

  • Write a Python program to compute the difference between the maximum and minimum integers formed by rearranging the digits of an 8-digit number.
  • Write a Python program to generate all permutations of an 8-digit input and calculate the difference between the largest and smallest resulting numbers.
  • Write a Python program to determine the difference between the highest and lowest numbers from a given set of 8 digits, preserving any leading zeros.
  • Write a Python program to find the maximum minus minimum value from all possible integer combinations derived from 8 input digits.

Go to:


Previous: Write a Python program to replace a string "Python" with "Java" and "Java" with "Python" in a given string.
Next: Write a Python program to compute the sum of first n given prime numbers.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.