Friday, 22 July 2011

Program for the below pattern without using 'for' loop

0
10
010
1010
01010

public class Design1 {

    private static int printNumber(int data) {
        System.out.print(data % 2);
        return data > 0 ? printNumber(data - 1) : 0;
    }
    private static int iterate(int noOfRows) {
        int currentnoOfRows = noOfRows > 0 ? iterate(noOfRows - 1) : 0;
        System.out.println();
        return printNumber(noOfRows);
    }
    public static void main(String[] args) {
        iterate(10);
        System.out.println();
    }
}

Divide two numbers without slash

public class Division {

    public static void main(String[] args) {
        int number1 = 10;
        int number2 = 3;
        int count = 0;
        while(number1 > number2) {
          number1 = number1 - number2;
          count++;
        }
        System.out.println(count);
    }
}

Tuesday, 19 July 2011

Subtract Two numbers without minus

public class Subtraction {

    public static void main(String[] args) {
       int number1 = 10;
       int number2 = 1;

       int result = ~(~number1+number2);
        System.out.println(result);
    }
}

Second Biggest Number

public class SecondMaximum {

    public static void main(String arg[]) throws Exception {
        int[] numbers = {87, 900, 500, 400, 700, 600, 300, 200,};
       
        int max = getMaxValue(numbers);
        System.out.println("Second Max number : "+ max);
    }

    public static int getMaxValue(int[] numbers) {
        int firstMax = numbers[0];
        int secondMax = 0;
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > firstMax) {
                firstMax = numbers[i];
            }
            else if(numbers[i] > secondMax && numbers[i] != firstMax){
                secondMax = numbers[i];
            }
        }
        return secondMax;
    }
}