Find the largest number possible by juggling the digits in a given integer.( e.g. I/p : 5923, O/p : 9532)

Chikul
Posted by Chikul under Others category on | Views : 2954
import java.io.*;
import java.lang.*;

public class Largest_Number
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a no : ");
String s=(br.readLine()).toString();
int l=s.length();
int i=Integer.parseInt(s);
int n[]=new int[l];
int x;
int c=0;

while(i!=0)
{
x=i%10;
n[c]=x;
i=i/10;
c++;
}
for(int j=0;j<n.length;j++)
{
for(int k=j+1;k<n.length;k++)
{
if(n[j]<n[k])
{
int t=n[j];
n[j]=n[k];
n[k]=t;
}
}
}
for(int j=0;j<n.length;j++)
{
System.out.print(n[j]);
}
}
}

Comments or Responses

Login to post response