WAP In java to accept and integer and print the digits in ascending order.
Answers
CODE :
import java.util.*;
class digits_ascending
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int cpy1=n;
int d=0;
int c=0;
while(cpy1>0)
{
cpy1=cpy1/10;
c++;
}
int cpy2=n;
int i=0;
int a[]=new int[c];
while(cpy2>0)
{
d=cpy2%10;
a[i]=d;
i++;
cpy2=cpy2/10;
}
int min=0;
int temp=0;
for(i=0;i<(c-1);i++)
{
min=i;
for(int j=i+1;j<c;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
System.out.println("The digits are printed in ascending :");
for(i=0;i<c;i++)
{
System.out.println(a[i]);
}
} //end of main
}//end of class
------------------------------------------------------------------------------------------------------
OUTPUT :
Enter a number
56123
The digits are printed in ascending :
1
2
3
5
6
Hope it helps :-)
____________________________________________________________________
import java.util.Scanner;
class ReverseNumberWhile
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
//While Loop: Logic to find out the reverse number
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of input number is: "+reversenum);
}
}
Hope helped