I want to arrange the digits of a number in descending order without using arrays. When I am using String it gives run time error. For example:
Input: 2436
Output: 6432
Here is what I wrote but the problem is the first digit isn't getting printed which is not the expected output.
public static void main (String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int num,n,r=0,FinalNo=0;
System.out.println("Enter any number");
num=sc.nextInt();
n=num;
while(n>0) {
r=n%10;
n=n/10;
FinalNo=(FinalNo*10)+r;
}
System.out.println("The number in descending order is "+FinalNo);
}