Saturday, February 19, 2011

BlueJ Program On Arranging Digits Of Number

Program to enter a number upto 8 digits and form a new number where odd digits get sorted in ascending order followed by even digits in descending order.



 import java.io.*;
class smith1
{
 int n,even[],odd[],i,evenindex=0,oddindex=0;
 String str;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeNumber() throws Exception
{
even=new int[8];
odd=new int[8];
do
{
 System.out.println("Enter the number:");
 str=br.readLine().trim();
 if(str.length()< =8)
 break;
 }while(true);
n=Integer.parseInt(str);
do
{
i=n%10;
if(i%2==0)
even[evenindex++]=i;
else
odd[oddindex++]=i;
n=n/10;
}while(n!=0);
sortOdd();
sortEven();
n=0;
for(i=0;i< oddindex;i++)
n=n*10+odd[i];
for(i=0;i< evenindex;i++)
n=n*10+even[i];
System.out.println("Modified Number="+n);
}
private void sortEven()
{
 int j,flag=0,t;
for(i=0;i< evenindex;i++)
{
flag=0;
for(j=0;j< evenindex-i-1;j++)
{
 if(even[j]< even[j+1])
 {
  t=even[j];
  even[j]=even[j+1];
  even[j+1]=t;
  flag=1;
  }
 }
 if(flag==0)
 break;
 }
 }
private void sortOdd()
{
 int j,flag=0,t;
for(i=0;i< oddindex;i++)
{
flag=0;
for(j=0;j< oddindex-i-1;j++)
{
 if(odd[j] >odd[j+1])
 {
  t=odd[j];
  odd[j]=odd[j+1];
 odd[j+1]=t;
  flag=1;
  }
 }
 if(flag==0)
 break;
 }
 }
 public static void main(String args[])throws Exception
 {
  smith1 obj=new smith1();
  obj.takeNumber();
  }
  }


Related Post: BlueJ Programs on Numeric Array
Related PostBlueJ Programs on Number

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner