Monday, 8 June 2015

Program to implement Binary search

//Program to implement Binary Search
import java.io.*;
public class Binary
{
  public static void main(String args[]) throws Exception
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int n, item, i, j, mid, top, bottom;
    int a[] = new int[100];

    System.out.print("Enter number of elements: ");
    n = Integer.parseInt(br.readLine());

    System.out.println("Enter "+n+" elements in ascending order: ");
    for (i = 0; i <= (n-1); i++) 
    {
      a[i]=Integer.parseInt(br.readLine());
    }

    System.out.print("\nEnter the element to search: ");
    item=Integer.parseInt(br.readLine());

    bottom = 0;
    top = n; 
    do 
    {
      mid = (bottom + top) / 2;
      if (item < a[mid])
      {
        top = mid - 1;
      }
      else if (item > a[mid])
      {
        bottom = mid + 1;
      }
    }while(item != a[mid] && bottom <= top);
     
    if (item == a[mid]) 
    {
      System.out.print("\n"+item+" is found at position: "+(mid+1));
    } 
    else 
    {
      System.out.print("\n"+item+" is not found");
    }
    System.out.println();
  }
}
//Program to implement Binary search
import java.io.*;  
import java.util.*;
public class Binary  
{  
  public static void main(String args[]) throws Exception  
  {  
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        
    int n, item, i;    
        
    System.out.print("Enter number of elements: ");  
    n = Integer.parseInt(br.readLine());  

    int a[] = new int[n];
        
    System.out.println("Enter "+n+" elements: ");  
    for (i = 0; i <= (n-1); i++)   
    {  
      a[i]=Integer.parseInt(br.readLine());  
    }  
       
    Arrays.sort(a);
    System.out.println("\nThe sorted list:");
    for (i=0; i<=(a.length - 1); i++) 
    {
      System.out.println(a[i]+"\t");
    }
        
    System.out.print("\nEnter the element to search: ");  
    item=Integer.parseInt(br.readLine());  

    int retVal = Arrays.binarySearch(a,item);   
    System.out.println("The index of element "+item+": "+(retVal+1));

    System.out.println();  
  }  
}    
Output:
Enter number of elements: 5
Enter 5 elements:
3
5
9
1
2

The sorted list:
1
2
3
5
9

Enter the element to search: 5
The index of element 5: 4

Arithmetic Operator Demo in Java

public class arithmetic-op
{
  public static void main(String args[])
  {
    i = 5;
    System.out.print("The Value of i: "+ i);
    
    i = i + 5;
    System.out.print("\nThe Value of i: "+ i);
    
    i = i - 2;
    System.out.print("\nThe Value of i: "+ i);
    
    i = i * 2;
    System.out.print("\nThe Value of i: "+ i);
    
    i = i / 4;
    System.out.print("\nThe Value of i: "+ i);
    
    i++;
    System.out.print("\nThe Value of i: "+ i);
    
    i++;
    System.out.print("\nThe Value of i: "+ i);
    
    i --;
    System.out.print("\nThe Value of i: "+ i);
    System.out.println();
  }
}
Output:
The Value of i: 5
The Value of i: 10
The Value of i: 8
The Value of i: 16
The Value of i: 4
The Value of i: 5
The Value of i: 6
The Value of i: 5

Sunday, 7 June 2015

Program to check whether two strings are anagrams of each other

import java.io.*;
public class Anagrams
{
  public static void main(String args[]) throws Exception
  {
    Anagrams a = new Anagrams();
    
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Enter first string: ");
    String str1 = br.readLine();
     
    System.out.print("Enter second string: ");
    String str2 = br.readLine();
    
    int flag = a.Anagram(str1, str2);

    if (flag==0)
    {
      System.out.print("\nResult: "+str1+" and "+str2+" are anagrams.");
    }
    else
    {
      System.out.print("\nResult: "+str1+" and "+str2+" are not anagrams.");
    }
    
    System.out.println();
  }


  public int Anagram(String str1,String str2)
  {
    if (str1.equals(str2))
    {
      return 0;
    }

    return 1;
  }
}
Output:
Enter first string: ana
Enter second string: ana

Result: ana and ana are anagrams.

Output2:
Enter first string: aana
Enter second string: ana

Result: aana and ana are not anagrams.

Program to Encrypt and Decrypt a Password

import java.io.*;
public class encrypt_decrypt
{
  public static void main(String args[]) throws Exception
  {
    encrypt_decrypt ed = new encrypt_decrypt();

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
    System.out.print("\nEnter the password: ");
    String password = br.readLine();

    //encryption process...
    StringBuilder str = new StringBuilder(password);
    password = ed.encrypt(str,0xFACA);
    System.out.print("\nEncrypted Password: " + password);

    //decryption process...
    str = new StringBuilder(password);
    password = ed.decrypt(str,0xFACA);
    System.out.print("\nDecrypted Password: " + password);

    System.out.println();
  }

  public String encrypt(StringBuilder str,int key)
  {
    for(int i=0; i<=(str.length() - 1); i++)
    {
      char c = (char)(str.charAt(i) - key);
      str.setCharAt(i,c);
    }
    return new String(str);
  }

  public String decrypt(StringBuilder str,int key)
  {
    for(int i=0; i<=(str.length() - 1); i++)
    {
      char c = (char)(str.charAt(i) + key);
      str.setCharAt(i,c);
    }
    return new String(str);
  }
}
Output:
Enter the password: admin

Encrypted Password: ?????
Decrypted Password: admin

Saturday, 6 June 2015

Program to generate prime numbers in a given range

import java.io.*;
public class Prime_no
{
  public static void main(String args[]) throws Exception
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int M, N, i, j, flag, temp, count = 0;
   
    System.out.print("\nEnter the value of M and N:\n");
    M = Integer.parseInt(br.readLine());
    N = Integer.parseInt(br.readLine());
   
    if(N < 2)
    {
      System.out.print("\nThere are no primes upto\n"+N);
    }

    System.out.print("\nPrime numbers are:");
    temp = M;
   
    if ( M % 2 == 0)
    {
      M++;
    }
    
    for (i=M; i<=N; i=i+2)
    {
      flag = 0;
   
      for (j=2; j<=i/2; j++)
      {
        if( (i%j) == 0)
        {
          flag = 1;
          break;
        }
      }
     
      if(flag == 0)
      {
          System.out.print("\n"+i);
          count++;
      }
    }
   System.out.println();
  }
}
Output
Enter the value of M and N:
1
10

Prime numbers are:
1
3
5
7

Conversion of numbers into words in Java

import java.io.*;  
public class num_to_words  
{  
  public static void main(String soham[]) throws Exception  
  {  
    long num,div,num1;  
    int flag=0,digit,position,total_digit;  
  
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  
    System.out.print("\nEnter number: ");  
    num = Integer.parseInt(br.readLine());  
  
    if(num==0)  
    {  
      System.out.print("Zero\n\n");  
    }  
  
    if(num>99999)  
    {  
      System.out.print("please enter number between 0 and 100000\n\n");  
    }  
  
    total_digit = 0;  
    div = 1;  
    num1 = num;  
    while ( num1 > 9 )  
    {  
      num1 = num1 / 10;  
      div = div * 10;  
      total_digit++;  
    }  
      
    total_digit++;  
    position = total_digit;  
    while ( num != 0 )  
    {  
      digit= (int)(num / div);  
      num = num % div;  
      div = div / 10;
        
      switch(position)  
      {  
        case 2:  
        case 5: 
            if ( digit == 1 )
            {
              flag = 1;  
            }
            else  
            {  
              flag = 0;  
              switch(digit)  
              {  
                case 2: System.out.print("twenty ");break;  
                case 3: System.out.print("thirty ");break;  
                case 4: System.out.print("forty ");break;  
                case 5: System.out.print("fifty ");break;  
                case 6: System.out.print("sixty ");break;  
                case 7: System.out.print("seventy ");break;  
                case 8: System.out.print("eighty ");break;  
                case 9: System.out.print("ninty ");  
              }  
            }  
            break;

        case 1:  
        case 4:    
            if ( flag == 1 )  
            {  
              flag = 0;  
              switch(digit)  
              {  
                case 0 : System.out.print("ten ");break;  
                case 1 : System.out.print("eleven ");break;  
                case 2 : System.out.print("twelve ");break;  
                case 3 : System.out.print("thirteen ");break;  
                case 4 : System.out.print("fourteen ");break;  
                case 5 : System.out.print("fifteen ");break;  
                case 6 : System.out.print("sixteen ");break;  
                case 7 : System.out.print("seventeen ");break;  
                case 8 : System.out.print("eighteen ");break;  
                case 9 : System.out.print("ninteen ");  
              }  
            }  
            else  
            {  
              switch(digit)  
              {  
                case 1 : System.out.print("one ");break;  
                case 2 : System.out.print("two ");break;  
                case 3 : System.out.print("three ");break;  
                case 4 : System.out.print("four ");break;  
                case 5 : System.out.print("five ");break;  
                case 6 : System.out.print("six ");break;  
                case 7 : System.out.print("seven ");break;  
                case 8 : System.out.print("eight ");break;  
                case 9 : System.out.print("nine ");  
              }  
            }  
            if ( position == 4 )  
            System.out.print("thousand ");  
            break;

        case 3:  
            if ( digit> 0 )  
            {  
              switch(digit)  
              {  
                case 1 : System.out.print("one ");break;  
                case 2 : System.out.print("two ");break;  
                case 3 : System.out.print("three ");break;  
                case 4 : System.out.print("four ");break;  
                case 5 : System.out.print("five ");break;  
                case 6 : System.out.print("six ");break;  
                case 7 : System.out.print("seven ");break;  
                case 8 : System.out.print("eight ");break;  
                case 9 : System.out.print("nine ");  
              }  
              System.out.print("hundred ");  
            }  
            break;  
      }  
      position--;  
    }  
  
    if ( position == 4 && flag == 0)  
    {  
      System.out.print("thousand");  
    }   
    else  
    {  
      if ( position == 4 && flag == 1)  
      {  
        System.out.print("ten thousand");  
      }  
    }  
      
    if ( position == 1 && flag == 1 )  
    {  
      System.out.print("ten ");  
    }  
      
    System.out.println();  
  }  
}  
Output1:
Enter number: 101
one hundred one

Output2:
Enter number: 1001
one thousand one

Pyramid Design in Java

    1
   232
  34543
 4567654
567898765
public class PyramidDesign
{
  public static void main(String soham[])
  {
    String str = "123456789";
    
    PyramidDesign pd = new PyramidDesign();

    for(int i=0; i<=4; i++)
    {
      pd.display(i,str);
      System.out.println();
    }
  }
  
  public void display(int start,String str)
  {
    int i,j;
    for(i=start; i<=(start+start); i++)
    {
      System.out.print(""+str.charAt(i));
    }
    
    for(j=(i-2); j >= start; j--)
    {
      System.out.print(""+str.charAt(j));
    }
  }
}