Monday, March 12, 2012

FindMissingNumber

/*Find out sum of n numbers and substract the sum of numbers from this sum.
 *
 *
 */
public class FindNumber {

    public static void main(String [] args)
    {
        int totalsum=10*(10+1)/2;
        int givensum=0;
        int [] a={1,4,3,2,5,7,9,8,10};
        for(int i=0;i<a.length;i++)
        {
            givensum +=a[i];
        }
        int missingnum=totalsum-givensum;
        System.out.println(" Missing number is :"+missingnum);
    }
}

Palindrome


import java.io.*;


public class Palindrome {

public static void main(String []args )
{
        palindromeEx ex=new palindromeEx();
        ex.palindrome(121);
        ex.palindrome("ADHCVBVCHDAA");
        ex.palind("ANANDNANA");
    }
}

class palindromeEx
{
    public void palindrome(int a)
    {
        //121
        int original=a;
        int reverse=0;
        while(a>0)
        {
            int b=a%10;
            reverse=reverse*10+b;
            a=a/10;
        }
        if(reverse==original)
        {
            System.out.println("Number is palindrome : "+reverse);   
        }
        else
            System.out.println("Number is not palindrome : "+reverse);

    }

    public void palinwithFor(int in)
    {
        int length=in;

        int rev=0;

        System.out.println("Entered number is:   "+in);

        for (int j=0;j<length;j++)

        {

            int num=length%10;

            length=length/10;

            rev=(rev*10)+num;

            j--;

        }

        System.out.println("Reverse number is:   "+rev);

        if(in==rev)

        {

            System.out.println("Number is palindrome");

        }

        else

            System.out.println("Number is not palindrome");
    }
    public void palindrome(String a)
    {
        String original=a;
        String reverse="";
        int length =a.length();
        for (int i=a.length();i>0;i--)
        {
            reverse=reverse+a.charAt(i-1);   
        }
        if(reverse.equalsIgnoreCase(original))
        {
            System.out.println("String is palindrome : "+reverse);   
        }
        else
            System.out.println("String is not palindrome : "+reverse);

    }

    public void palind(String a)
    {
        String original=a;
        String reverse=new StringBuffer(a).reverse().toString();
        if(reverse.equalsIgnoreCase(original))
        {
            System.out.println("String is palindrome : "+reverse);   
        }
        else
            System.out.println("String is not palindrome : "+reverse);

    }

}

Sunday, March 11, 2012

Nth Largest number in List


import java.io.*;

public class NthLargest {

public static void main(String args[])
{
int A[] = new int[]{6,5,2,7,3,8,32,1,43,87,45};
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter number");
try {
int n = Integer.parseInt(reader.readLine());
int res = 0;
for(int i=0;i<A.length;i++)
{
int k = 0;
for(int j=0;j<A.length;j++)
{
if(A[i]<A[j])
++k;
if((k==(n-1))&&(j==A.length-1))
{
res = A[i];
i = A.length;
}
}
}
System.out.println("The "+n+"th largest number is  :"+res);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//This is the supposed nth largest number,here it is third
}
}

Largest/Smallest number in array



import java.util.ArrayList;
public class SmallestinList {
public static void main(String []args)
{
int []a = {12,23,15,34,45,56,67,89,80,8,80,11};
ArrayList<Integer> list=new ArrayList<Integer>();
//list=Arrays.asList(a);
for(int i=0;i<a.length;i++)
{
list.add(a[i]);
}
//Using sort method of collection class
/*Collections.sort(list);
System.out.println("The sorted list is"+list.toString());
int n=list.size();
System.out.println(" Length of list "+n);
System.out.println("The smallest number in list is:  "+list.get(0));
System.out.println("The Largest number in list is:  "+list.get(n-1));
System.out.println("The second largest number in list is:  "+list.get(n-2));*/
//Self implementation
int smallest=list.get(0);
int largest=list.get(0);
for (int i=0;i<list.size()-1;i++)
{
if(smallest>list.get(i+1))
{
smallest=list.get(i+1);
}
if(largest<list.get(i+1))
{
largest=list.get(i+1);
}
}
System.out.println("The smallest number in list is:  "+smallest);
System.out.println("The Largest number in list is:  "+largest);
}
}


Saturday, March 10, 2012

CountFilesinDir



import java.io.*;

public class CountFilesInDirectory {
static int count;
        public static void main(String[] args) {
                File f = new File("C:/Users/anandy/Downloads/Resume");
                countFile(f);
                System.out.println("Number of files: " + count);
        }
       
        public static void countFile(File fdir)
        {
            for (File file : fdir.listFiles()) {
                if (file.isFile()) {
                        count++;
                }
                else
                {
                String path = file.getAbsolutePath();
//Extra check for backward /
              //String absPath= path.replace('\\', '/');
               //System.out.println(path);
               //System.out.println(absPath);
               File f1=new File(path);
                countFile(f1);
                }
        }
        }
}

Tuesday, March 6, 2012

Interactive program for Factor

import java.io.*;
public class Factor {

    public static void main(String [] args)
    {
        getFactor();
        while(true)
        {
        System.out.print("Do you want another number?(YES/NO)  ");
        BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
        try {
            String decision = reader.readLine();
            if(decision.equalsIgnoreCase("YES")|| decision.equalsIgnoreCase("Y"))
            {
                getFactor();
            }
            else
            {
                System.out.println("Exiting...");
                break;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }
    public static void getFactor()
    {
    BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
    try {
        System.out.print("Please enter a number for factor:  ");
        int n=Integer.parseInt(reader.readLine());
       
        for(int i=2;i<=n;i++)
        {

      if(i>n/2)
{
i=n;
}

            while (n%i==0)
            {
                System.out.println("  "+i);
                n=n/i;
            }
        }
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
    }

Thursday, March 1, 2012

JDBCSample

import java.sql.*;
class JDBCSample {
  public static void main (String[] args) throws Exception
  {
   Class.forName("oracle.jdbc.OracleDriver");

   /*Connection conn = DriverManager.getConnection
     ("jdbc:oracle:thin:@//localhost:1521/orcl", "system", "admin");
                        // @//machineName:port/SID,   userid,  password*/
   Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","scott", "tiger");
   try {
     Statement stmt = conn.createStatement();
     try {
       ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
      // ResultSet rset = stmt.executeQuery("select 'Shalini' from dual");
       try {
         while (rset.next())
           System.out.println (rset.getString(1));   // Print col 1
       }
       finally {
          try { rset.close(); } catch (Exception ignore) {}
       }
     }
     finally {
       try { stmt.close(); } catch (Exception ignore) {}
     }
   }
   finally {
     try { conn.close(); } catch (Exception ignore) {}
   }
  }
}