Monday, December 2, 2002

Floyd Triangle

package org.interview.test;

import java.util.Scanner;

public class FloydTriangle {

/*
*In Floyd triangle there are n integers in the nth row and a total of (n(n+1))/2 integers 
*in n rows. This is a simple pattern to print but helpful in learning how to create other 
*patterns. Key to develop pattern is using nested loops appropriately.
*/
public static void main (String [] args) throws Exception {
int n, num = 1, c, d;
     Scanner in = new Scanner(System.in);
 
     System.out.println("Enter the number of rows of floyd's triangle you want");
     n = in.nextInt();
 
     System.out.println("Floyd's triangle :-");
 
     for ( c = 1 ; c <= n ; c++ )
     {
        for ( d = 1 ; d <= c ; d++ )
        {
           System.out.print(num+" ");
           num++;
        }
 
        System.out.println();
     }
}
}

F A C T O R I A L

package org.interview.test;

import java.util.Scanner;

public class Factorial {

public static void main (String [] args) throws Exception {
findFactorial();
}

public static Integer findFactorial() {
 int n , fact = 1;
 
 Scanner sc = new Scanner(System.in);
 
 System.out.println("Enter number to calculate Factorial: ");
 n = sc.nextInt();
 
 for (int i = 1 ; i <= n ; i++) {
 fact *= i;
 }
 System.out.println("Factorial: " + fact);
 return fact;
}

}

C A L C U L A T O R

package org.interview.test;

import java.util.Scanner;

public class Calculator {

public static void main (String []  args) throws Exception {

Scanner sc = new Scanner(System.in);

System.out.println("First Number:");
int x = sc.nextInt();
System.out.println("Second Number:");
int y = sc.nextInt();

System.out.println("Mathematical Operation:");
String op = sc.next();

if(op.equalsIgnoreCase("/")) {
System.out.println(x/y);
}else if(op.equalsIgnoreCase("*")) {
System.out.println(x*y);
}else if(op.equalsIgnoreCase("+")) {
System.out.println(x+y);
}else if(op.equalsIgnoreCase("-")) {
System.out.println(x-y);
}else if(op.equalsIgnoreCase("eo")) {
if(x%2==0) {
System.out.print("X is Even");
} else {
System.out.println("X is Odd");
}
if(y%2==0) {
System.out.print("Y is Even");
} else {
System.out.println("Y is Odd");
}
}

}
}

Sunday, December 1, 2002

Bubble Sort

package org.interview.test;

import java.util.Scanner;

public class BubbleSort {

/**
* Complexity of bubble sort is O(n2) which makes it a less frequent option for 
* arranging in sorted order when quantity of numbers is high.
* @param args
*/
public static void main(String []args) {
   int n, c, d, swap;
   Scanner in = new Scanner(System.in);
 
   System.out.println("Input number of integers to sort");
   n = in.nextInt();
 
   int array[] = new int[n];
 
   System.out.println("Enter " + n + " integers");
 
   for (c = 0; c < n; c++) 
     array[c] = in.nextInt();
 
   for (c = 0; c < ( n - 1 ); c++) {
     for (d = 0; d < n - c - 1; d++) {
       if (array[d] > array[d+1]) /* For descending order use < */
       {
         swap       = array[d];
         array[d]   = array[d+1];
         array[d+1] = swap;
       }
     }
   }
 
   System.out.println("Sorted list of numbers");
 
   for (c = 0; c < n; c++) 
     System.out.println(array[c]);
 }

}

Binary Search

package org.interview.test;

import java.util.Arrays;
import java.util.Scanner;

public class BinarySearch {

public static void main (String [] args) throws Exception {
binarySearch();

binarySearch02();
}



public static void binarySearch02(){


int c, first, last, middle, n, search, array[];
 
   Scanner in = new Scanner(System.in);
   System.out.println("Enter number of elements");
   n = in.nextInt(); 
   array = new int[n];
 
   System.out.println("Enter " + n + " integers");
 
 
   for (c = 0; c < n; c++)
     array[c] = in.nextInt();
 
   System.out.println("Enter value to find");
   search = in.nextInt();
 
   first  = 0;
   last   = n - 1;
   middle = (first + last)/2;
 
   while( first <= last )
   {
     if ( array[middle] < search )
       first = middle + 1;    
     else if ( array[middle] == search ) 
     {
       System.out.println(search + " found at location " + (middle + 1) + ".");
       break;
     }
     else
        last = middle - 1;
 
     middle = (first + last)/2;
  }
  if ( first > last )
     System.out.println(search + " is not present in the list.\n");
  
  
}

/**
* binarySearch method returns the location if a match occurs otherwise -(x+1) 
* where x is the no. of elements in the array, For example in the second case 
* above when p is not present in characters array the returned value will be -6.
*/
public static void binarySearch (){
char characters[] = { 'a', 'b', 'c', 'd', 'e' };
 
   System.out.println(Arrays.binarySearch(characters, 'a'));
   System.out.println(Arrays.binarySearch(characters, 'c'));
   System.out.println(Arrays.binarySearch(characters, 'p'));
}
}

Armstrong Number

package org.interview.test;

import java.util.Scanner;

public class ArmstrongNumber {

/*
* An Armstrong number of three digits is an integer such that the sum of the cubes of its 
*  digits is equal to the number itself. 
* For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
*/
public static void main (String [] args ) throws Exception {
int n, sum = 0, temp, r;
 
     Scanner in = new Scanner(System.in);
     System.out.println("Enter a number to check if it is an armstrong number");      
     n = in.nextInt();
 
     temp = n;
     System.out.println("temp:"+temp);
     int c =1;
     while( temp != 0 ) {
     System.out.println(c+".temp:"+temp);
        r = temp%10;
        System.out.println(c+".r:"+r);
        sum = sum + r*r*r;
        System.out.println(c+".sum:"+sum);
        temp = temp/10; 
        System.out.println(c+".temp:"+temp);
        c++;
     }
 
     if ( n == sum )
        System.out.println("Entered number is an armstrong number.");
     else
        System.out.println("Entered number is not an armstrong number.");
}

}

Add Matrix

package org.interview.test;

import java.util.Scanner;

public class AddMatrix {
public static void main(String args[])
  {
     int m, n, c, d;
     Scanner in = new Scanner(System.in);
 
     System.out.println("Enter the number of rows and columns of matrix");
     m = in.nextInt();
     n  = in.nextInt();
 
     int first[][] = new int[m][n];
     int second[][] = new int[m][n];
     int sum[][] = new int[m][n];
 
     System.out.println("Enter the elements of first matrix");
 
     for (  c = 0 ; c < m ; c++ )
        for ( d = 0 ; d < n ; d++ )
           first[c][d] = in.nextInt();
 
     System.out.println("Enter the elements of second matrix");
 
     for ( c = 0 ; c < m ; c++ )
        for ( d = 0 ; d < n ; d++ )
           second[c][d] = in.nextInt();
 
     for ( c = 0 ; c < m ; c++ )
        for ( d = 0 ; d < n ; d++ )
            sum[c][d] = first[c][d] + second[c][d];  //replace '+' with '-' to subtract matrices
 
     System.out.println("Sum of entered matrices:-");
 
     for ( c = 0 ; c < m ; c++ )
     {
        for ( d = 0 ; d < n ; d++ )
           System.out.print(sum[c][d]+"\t");
 
        System.out.println();
     }
  }
}