Plz give a program in JAVA to take an integer as input and print the digits in ascending order.
Plz give as fast as possible.
Answers
CODE :
import java.util.*;
class digits_ascending
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int cpy1=n;
int d=0;
int c=0;
while(cpy1>0)
{
cpy1=cpy1/10;
c++;
}
int cpy2=n;
int i=0;
int a[]=new int[c];
while(cpy2>0)
{
d=cpy2%10;
a[i]=d;
i++;
cpy2=cpy2/10;
}
int min=0;
int temp=0;
for(i=0;i<(c-1);i++)
{
min=i;
for(int j=i+1;j<c;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
System.out.println("The digits are printed in ascending :");
for(i=0;i<c;i++)
{
System.out.println(a[i]);
}
}
//end of main
}//end of class
------------------------------------------------------------------------------------------------------
OUTPUT :
Enter a number
9876
The digits are printed in ascending :
6
7
8
9
Hope it helps :-)
____________________________________________________________________