/* Author : Dharmendra Kumar Sahu Modified Date: 10 Mar 2010 Email Id : dknitk@rediffmail.com */ 1.

Divisor of a Number /* This program will generate all the possible divisor of a given number. */

package subexinterview; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Divisor { public static void main(String[] args) throws NumberFormatException, IOException{ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please Enhter a Number:"); int a = Integer.parseInt(bufferedReader.readLine()); List temp = factor(a); System.out.println("Factor of the number are:"+temp); for(int i=0;i<temp.size();i++){ System.out.println(temp.get(i)); } } public static List factor(int a){ List list = new ArrayList(); for(int i=2;i<a;i++){ if(a%i==0){ list.add(i); } } return list;

} }

2. Factors of a Number /* This Program will generate Factor of a given number. */ package subexinterview; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class FactorOfNumber{ public static void main(String[] args)throws NumberFormatException, IOException{ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please Enter a number:"); int a = Integer.parseInt(bufferedReader.readLine()); List temp = factor(a); if(temp.size() !=0){ for(int i=0;i<temp.size();i++){ System.out.println(temp.get(i)); } }else{ System.out.println("****This Number does not have any factors****"); } } public static List factor(int num){ int temp = 1; int tempnum = num; List list = new ArrayList(); for(int i=2;i<tempnum;i++){

if(num%i==0){ list.add(i); num = num/i; temp*=i; i--; } if(temp == tempnum){ break; } } return list; } }

3. Get vowel from a string

/* This program will get all the vowel from a given String. */ package subexinterview; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class GetVowel{ public static void main(String[] args){ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); //System.out.println("Enter a String"); String str = "Ramkumarsign"; List temp = getVowel(str); if(temp.size() !=0){ for(int i=0;i<temp.size();i++){ System.out.println(temp.get(i)); } }else{

System.out.println("****This String does not have any Vowels****"); } System.out.println("Vowel With Index number:"); Map tempMap = getVowelWithIndex(str); if(tempMap.size() !=0){ System.out.println(tempMap); }else{ System.out.println("****This String does not have any Vowels****"); } } public static List getVowel(String str){ List list = new ArrayList(); for(int i=0;i<str.length();i++){ if(str.charAt(i)=='a' || str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u'){ list.add(str.charAt(i)); } } return list; } public static Map getVowelWithIndex(String str){ Map map = new HashMap(); for(int i=0;i<str.length();i++){ if(str.charAt(i)=='a' || str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u'){ map.put(i,str.charAt(i)); } } return map; } }

4. Quick Sort /* This program is an implementaion of quick sort. */

package algorithms; public class QuickSort { int numbers[]; int number; public void sort(int[] values){ this.numbers = values; number = numbers.length; quichSortAlgo(0,number-1); System.out.println("Sorted Arrays"); for(int k : numbers){ System.out.println(k); } } public int[] sortReturn(int[] values){ this.numbers = values; number = numbers.length; quichSortAlgo(0,number-1); /*for(int k : numbers){ System.out.println(k); }*/ return numbers; } private void quichSortAlgo(int low, int high){ int i= low, j=high; int pivot = numbers[(low+high)/2]; while(i<=j){ while(numbers[i]< pivot){ i++; } while(numbers[j]>pivot){ j--; } if(i<=j){ exchange(i,j); i++; j--; } } if(low < j){ quichSortAlgo(low, j); } if(high >i){ quichSortAlgo(i, high); }

} private void exchange(int i, int j){ int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; } public static void main(String args[]){ int values[] = {10,20,5,60}; QuickSort quickSort = new QuickSort(); quickSort.sort(values); for(int value : values){ System.out.println(value); } } } 5. Lst Of Month /* This program will give last day of the given date. */ package algorithms; import java.sql.Date;

public class LastDayofMonth { private int lastDayOfMonth(Date date){ int lastday = Integer.valueOf(date.toString().substring(0,2)); return lastday; } public static void main(String args[]){ System.out.println(new LastDayofMonth().lastDayOfMonth(new Date(2009-10-10))); } }

6. Bubble Sort and Selection Sort

/* This program is an implementaion of Bubble Sort and Selection Sort. */ package datastructure; import java.util.Scanner; public class BubbleSort { public static void main(String args[]){ int a[] = {10,25,2,11,14}; int n=a.length; Scanner scanner = new Scanner(System.in); System.out.println("Enter the option for sorting - -"); System.out.println("1. Buuble Sort"); System.out.println("2. Selection Sort"); System.out.println("3. Intersion Sort"); System.out.println("10. Exit"); int c = scanner.nextInt(); switch(c){ case 1: bubbleSort(a,n); break; case 2: selectionSort(a, n); break; case 3: insetionSort(a,n); break; default: System.out.println("Not Valid option"); System.exit(0); } } public static void bubbleSort(int[] a, int n){ for(int i=1;i<n;i++){ for(int j=0;j<n-i;j++){ if(a[j]>a[j+1]){ int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } }

} System.out.println("----- Bubble Sort ----"); for(int b : a){ System.out.println(b); } } public static void selectionSort(int[] a, int n){ int pos,small; for(int i=0;i<n;i++){ small = a[i]; pos = i; for(int j=i+1;j<n;j++){ if(small > a[j]){ small = a[j]; pos = j; } } int temp = a[pos]; a[pos] = a[i]; a[i] = temp; } System.out.println("----- Selectionn Sort ----"); for(int b : a){ System.out.println(b); } } public static void insetionSort(int[] a, int n){ for(int i=1;i<n;i++){ int temp = a[i]; int j = i; while(j>0 && a[j-1] >=temp){ a[j] = a[j-1]; --j; } a[j] = temp; } System.out.println("----- Insertion Sort ----"); for(int b : a){ System.out.println(b); } } }