Wednesday, January 1, 2020

BlueJ Program To Take a number and show it in ascending order of digits



import java.util.*;
class Asc
{

int i,j,t,n,x=0,arr[];
 public void show(int no)
 {
     n=no;
     arr=new int[10];
     for(i=n;i >0;i=i/10)
     {
          arr[x++]=i%10;
      }
      for(i=0;i< x-1;i++)
      {
           for(j=i+1;j< x;j++)
           {
                if(arr[i] > arr[j])
                {
                    t=arr[i];
                    arr[i]=arr[j];
                    arr[j]=t;
                }
            }
        }
        n=0;
        for(i=0;i< x;i++)
        {
             n=n*10+arr[i];
         }
     
         System.out.print("\nNumber in ascending order: "+n);    
  }
      public static void main(String args[])
{
     Scanner sc=new Scanner(System.in);
     Asc ob=new Asc();
     int n=sc.nextInt();
     ob.show(n);
}
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner