Java: Find the difference between the largest integer and the smallest integer which are created by 8 numbers from 0 to 9
Java Basic: Exercise-231 with Solution
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:
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:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to replace a string "python" with "java" and "java" with "python" in a given string.
Next: Write a Java program to compute the sum of first n given prime numbers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/basic/java-basic-exercise-231.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics