Categories

# Arrange Digits in Descending Order in Java

Write a Java program to allow the user to input a positive integer.

Now form a new integer by rearranging the digits in descending order.

Display the new integer formed.

Example:
Sample Input: 6729
Sample Output:9762

import java.io.*;
class Arrange{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a positive integer: ");
int n = Math.abs(Integer.parseInt(br.readLine()));
int len = Integer.toString(n).length();
int a[] = new int[len];
int num = n;
int i = 0;
while(num != 0){
a[i] = num % 10;
num /= 10;
i++;
}
for(i = 0; i < len; i++){
for(int j = 0; j < len - 1 - i; j++){
if(a[j] < a[j + 1]){
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
String result = "";
for(i = 0; i < len; i++)
result += a[i];
int desc = Integer.parseInt(result);
System.out.println(desc);
}
}

## 2 replies on “Arrange Digits in Descending Order in Java”

sayan ranasays:

Sir,a program is given which states that
WAP in JAVA to accept a positive integer and check whether the number is automorphic or not by using recursive function.
In mathematics, an automorphic number is a number whose square “ends” in the same digits as the number itself. For example, 52 = 25, 62 = 36, 762 = 5776

Input : N = 76
Output : Automorphic
Explanation: As 76*76 = 5776

adminsays:

Here is the link to the solution of your question. Please share this website with your friends as well.

This site uses Akismet to reduce spam. Learn how your comment data is processed.