Write a program to Sort the Digits of a Number in Ascending Order
import java.util.Scanner; public class SortDigitsInInteger { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter a number:-"); int num=sc.nextInt(); int a,i=0; int sort[]=new int[10]; while(num>0) { a=num%10; sort[i]=a; i++; num=num/10; } for(int j=0;j<i-1;j++) for(int k=j+1;k<i;k++) if(sort[j]>sort[k]) { int temp=sort[j]; sort[j]=sort[k]; sort[k]=temp; } System.out.println("The digits in number ascending order:"); for(int j=0;j<i;j++) System.out.print(sort[j]+" "); } } OUTPUT: Enter a number:- 4261 The digits in number ascending order: 1 2 4 6
No comments:
Post a Comment