w3resource

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


Difference Between Largest and Smallest Integer

Write a Java program to find the difference between the largest integer and the smallest integer. These integers are created by 8 numbers from 0 to 9. The number that can be rearranged starts 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:

Java Basic Exercises: Find the difference between the largest integer and the smallest integer which are created by 8 numbers from 0 to 9.


Sample Solution:

Java Code:

// Importing necessary classes for input/output operations and array manipulation
import java.util.*;

// Main class named "Main"
public class Main {
    
    // Main method to execute the program
    public static void main(String[] args) {
        // Creating Scanner object to read input from the user
        Scanner sc = new Scanner(System.in);
        
        // Prompting the user to input an integer created by 8 numbers from 0 to 9
        System.out.println("Input an integer created by 8 numbers from 0 to 9:");
        
        // Reading the input string
        String s = sc.next();
        
        // Initializing an array to store individual digits of the input integer
        int[] num = new int[8];
        
        // Extracting each digit from the input string and storing it in the array
        for (int i = 0; i < 8; i++) {
            num[i] = Integer.valueOf(s.substring(i, i + 1));
        }
        
        // Sorting the array in ascending order
        Arrays.sort(num);
        
        // Initializing variables to calculate the smallest and largest integers
        int a = 0;
        int b = 0;
        int c = 1;
        
        // Calculating the smallest and largest integers from the sorted array
        for (int i = 0; i < 8; i++) {
            a += num[i] * c;
            b += num[7 - i] * c;
            c *= 10;
        }
        
        // Outputting the difference between the largest and smallest integers
        System.out.println("Difference between the largest and the smallest integer from the given integer:");
        System.out.println(a - b);
    }
} 

Sample Output:

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

Flowchart:

Flowchart: 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 Java program to compute the difference between the largest and smallest numbers in an array after removing duplicates.
  • Write a Java program to find the difference between the largest and smallest integers formed by rearranging the digits of a given number.
  • Write a Java program to calculate the difference between the maximum and minimum values in a sequence while excluding outliers.
  • Write a Java program to compute the difference between the largest and smallest integers in an array sorted using a custom comparator.

Go to:


PREV : Replace "python" with "java" and Vice Versa.
NEXT : Sum of First n Prime Numbers.

Java Code Editor:

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.