Saturday 22 May 2010

Sorting DecendingOrder by Using TreeSet Collection

import java.util.*;

public class SortingDescending {
  @SuppressWarnings("unchecked")
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] num = new int [10];
    int count = 0;
while(count< num.length){
      num[count]= input.nextInt();
      ++count;
     }
    TreeSet tree = new TreeSet()      ;  

for(int i = 0; i < num.length; i++){
tree.add(num[i])        ;
      }
System.out.println (" The numbers of the array in descrending order are: " +  tree.descendingSet() );
   }
}
OtterBox Defender Case for iPhone 3G, 3G S (Black)

Monday 26 April 2010

Find Prime Factors


import java.util.Scanner;

public class PrimeFactor{

public static void main(String [] args){

int number = 1, check = 0;

Scanner input = new Scanner(System.in);

while(check != 1){

System.out.println("Please enter an integer");

number = input.nextInt();

if(number == 0){

check = 1;

}

else if(number < 0){

System.out.print("-1 ");

number = number * -1;

while(number > 1){

int count = 2;

while(count <= number){

if(number% count == 0){

number = number / count;

System.out.print(" x " + count);

Convert Binary to Decimal



import java.util.Scanner;

public class BinaryToDecimal {

    public static void main(String[] args) {
     
        System.out.println("Enter a binary number: ");
        Scanner scanStr = new Scanner(System.in);
             
        int decimalNum = 0;     
             
        if(scanStr.hasNext())
        {  
            try
            {
                decimalNum = Integer.parseInt(scanStr.next(),2);
     System.out.println("Binary number in decimal is: "+decimalNum); 
            }
            catch(NumberFormatException nfe)
            {
                System.out.println("The number is not a binary number.");
            }
        } 
    }
}