Find us on Google+ Kill the code: November 2012

Sunday 18 November 2012

Store Credit in JAVA

This program help customer to buy the most expensive products from all the available in the shop. This program takes from user the available amount with him and cost of all the available products in the shop. Than it finally gives the index number of the products that are most suitable.



public class store_credit {
    public static void main(String[] args) {
        int credit = 100; /*This is the available credit to the user.*/
    int total = 3; /*This is the total number of the products in shop.*/
    int price[] = {5,75,25}; /*This is the price array of all the items.*/
    int size = total;
    int temp = 0;
    int i;
    int pos1=0,pos2=0;
    for(i=0;i<size;i++) {
            for(int j=0;j<size;j++) {
                int t = price[i]+price[j];
        if( t > temp && t <= credit && i != j) {
                    temp = t;
                    pos1 = i + 1;
                    pos2 = j + 1;
        }
        }
    }
        System.out.println("Case#1: "+pos1 + " "+pos2);
    }
}

Reverse String Dramatically in JAVA

Input    : my name is nisarg mehta
Output : mehta nisarg is name my


public class reverse_string {
    public static void main(String args[]) {
        String s = "my name is nisarg mehta";
        String output = "";
        int space = 4;
        String temp = "";
        int i = s.length();
        int j = i;
        while(space >= 0) {
            while(i>=1 && s.charAt(--i) != ' ') {
            }
            temp = s.substring(i,j);
            j = i;
            output += " " + temp;
            temp = "";
            space--;
        }
        System.out.println("Case#1 : "+output);
    }
}