September 2012

Saturday, September 15, 2012

Hexa-Decimal Coversion





   Hex Value             Decimal Value 
  0                         0
  1                         1
  2                         2
  3                         3
  4                         4
  5                         5
  6                         6
  7                         7
  8                         8
  9                         9
   A                         10
   B                         11
   C                         12
   D                         13
   E                         14
   F                         15
   .                          .
   .                          .
 so on ...........                    

  



Maximum Number (Retrieve Maximun Number Of Array)







public class MaximumNumber 
{
      public static void main(String[] args)
      {
       int[] numbers = {3,4,5,7};
        int max = 0;      

        for(int i=0;i<numbers.length;i++)
        {
           if(numbers[i] > max)
           {
               max = numbers[i];
           }
       } 
       System.out.println("Max Number In Array : " + max);
   }

}


EvenOdd-Number (Determine Even-Odd Number - Try Catch Handler)




import java.io.*;

public class EvenOddNumber{
    public static void main(String[] args) throws IOException{
    try{
       int n;
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.print("Enter Number : "); 
    n = Integer.parseInt(in.readLine()); 
    if (n % 2 == 0){
       System.out.println("Given number is Even.");
    }else{    
       System.out.println("Given number is Odd.");
    }
     }catch(NumberFormatException e){
     System.out.println(e.getMessage() + " is not a numeric value.");
           System.exit(0);
     }
    }
}


Anagrams ( Creating Anagrams -Entered Words )





import  java.io.*;
 
public class Anagrams
{
   public static void main(String[] args)throws IOException
   {
       BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
          System.out.print("Enter a string:"); 
          String s = r.readLine(); 
              
          char[] text = new char[s.length()]; 
       for (int i=0; i<s.length(); i++)
       {
           text[i] = s.charAt(i);
       }
       System.out.println("Here are all the anagrams of " + s);
       printAnagrams(text, 0); 
   
   }
   
   public static void printAnagrams(char[] a, int i)
   {
       if (i == a.length-1)  printArray(a);
       else {
           for (int j=i; j< a.length; j++) {
               //swap a[i] with a[j]
               char c = a[i]; 
               a[i] = a[j]; 
               a[j] = c;
               printAnagrams(a, i+1);
               //swap back
               c = a[i]; 
               a[i] = a[j]; 
               a[j] = c;
           }
       }
   }
   static void printArray(char [] a)
   {
       for (int i=0; i< a.length; i++) System.out.print(a[i]); 
       System.out.println();
   } 
}