Q.Create a class NS to enable the user to sort the digits of an integer number
Data Member:
n-> To store the integer number
ar[]->array to store the digits of number
Member Method:
NS(int x) ... Parameterised constructor to initialize x
void extract_digit() >>> Extract the digit of the number & store it into the arrow
void short() ... To Short the elements in array
void display().... To Display the original numbers and sorted numbers
import java.util.*;
class ns
{
int n;
int ar[];
int C=0;
ns(int x)
{
n=x;
int num=n;
while (num>0)
{
num=num/10;
C++;
}
ar=new int[C];
}
void extract_digit()
{
int num=n;
int dx=0;
while (num>0)
{
int d=num%10;
num=num/10;
ar[dx]=d;
dx++;
}
}
void sort()
{
for(int i=0;i<=(C-2);i++)
{
for(int j=(i+1);j<C;j++)
{
if(ar[i]>ar[j])
{
int t=ar[i];
ar[i]=ar[j];
ar[j]=t;
}
}
}
}
void display()
{
System.out.println(n);
System.out.println();
for(int i=0;i<C;i++)
{
System.out.print(ar[i]);
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Integer");
int x1=sc.nextInt();
ns ob=new ns(x1);
ob.extract_digit();
ob.sort();
ob.display();
}
}