Please answer as soon as possible. Give the output either in JDK or BLUEJ but it should work on both.
Simple logics needed. Please don't use any library classes or functions.
Write a program in Java to input a number and arrange it's digits in ascending order.
Answers
Answered by
6
//Program to arrange digits in ascending order
import java.util.*;
class abc{
public static void main (String ar[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int N1=0,N2=n;
for(int I=0;I<=9;I++){
while(n!=0){
if(n%10==I)
N1=N1*10+(n%10);
n/=10;
}
n=N2;
}
System.out.println("New number:"+N1);
}
}
Logic:-
- Run I loop from 0 to 9
- check if digits of number = I
- True? Put that digit in variable N2
- After I loop ends, print the new number
Note:-
- Don't copy-paste the exact códe as there are some empty characters used to show indentation.
Similar questions