Tuesday, November 22, 2011

BlueJ Program On Arranging The Digits Of A Number In Ascending Order


This is a BlueJ program  on rearranging the digits of number in ascending order. Suppose the entered number is 2314 then the output will be 1234.

Codes of the program

import java.io.*;
class Num1
{
int arr[]=new int[10];
int i,j,t,n,x=0,rev=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws IOException
{
 System.out.print("Enter the Number:");
 n=Integer.parseInt(br.readLine());
System.out.println("Entered Number="+n);
while(n>0)
{
 arr[x++]=n%10;
 n=n/10;
}

for(i=0;i< x-1;i++)
{
for(j=i+1;j< x;j++)
{
if(arr[i]>arr[j])
{
 t=arr[i];
 arr[i]=arr[j];
 arr[j]=t;
}
}
}
for(i=0;i< x;i++)
{
 rev=rev*10+arr[i];
}
System.out.println("The number in modified form="+rev);
}

public static void main(String args[]) throws Exception
{
 Num1 ob=new Num1();
 ob.take();
}
}
In this program, digits from the entered number are retrieved and stored in an array. The array is then sorted in ascending order and from the array the digits are retrieved and a new number is formed.

Sample input and output of the program

Enter the Number:261
Entered Number=261
The number in modified form=126

Enter the Number:9715
Entered Number=9715
The number in modified form=1579

2 comments:

  1. could you provie me with a programme on dequeue

    ReplyDelete
  2. Please check my posting on 13 December 2011.

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner