Friday, 1 February 2013

Java beginner : Print a number pyramid

the format of the pyramid should be like this:
      1
    123
  12345
1234567


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package printpyramid;

/**
 *
 * @author Sunshine
 */
public class PrintPyramid {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int n=8;
        for(int i=1;i<n;i=i+2){
            for(int j=1;j<i+1;j++){
              //  System.out.println("");
                System.out.println(j);            
               
            }
            System.out.println(" ");
        }
    }
}

Java Interview : Insert 1-100 numbers in an array of 99 cell and find out the missing number


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package arrayof99cells;

import java.io.IOException;

/**
 *
 * @author Sunshine
 */
public class Arrayof99cells {
    int FindMinssing(int[] array){
        int sum=0;
        int sum_of_n_natural_nums=((array.length+2)*(array.length+1))/2;
        System.out.println(array.length);
        for(int i=0;i<array.length;i++){
            sum_of_n_natural_nums-=array[i];
        }
        return (sum_of_n_natural_nums);
       
    }
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        int[] arrayof99cells=new int[]{1,2,3,5};
        Arrayof99cells a=new Arrayof99cells();
        int missing_number=a.FindMinssing(arrayof99cells);
        System.out.println(missing_number);
       
       
    }
}

Saturday, 29 December 2012

Doomsday Special: Doomsday aftermath



For all those who waited for the doomsday to happen and are upset with the fact that it finally DID NOT actually take place. I have something really interesting to offer to bring your interest back to the world which apparently DID NOT end.

Let’s see the 3 major pluses for doomsday not actually happening:
1.       You get to use your apple gadgets…wallah! You still get to use your iPhone, IPad
2.       Surviving the post Doomsday destroyed  world is much more difficult then surviving this world…. So what if you don’t not get to rob all the Apple stores!
3.       Living on a totally destroyed planet under the pressure of building it all over again …well  trust me it would have been real pain in the a**  … so thank God that he saved you from all that pain .


So i hope finally 'to some extent' you will be able to live with the fact that yippiiiiiee ! doomsday did not happen :)


Monday, 5 November 2012

In 3 Different Integer Array Print the Combination of 3 Array Element that Sum upto Zero


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg3arraysum;

/**
 *
 * @author Sunshine
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int a1[] = new int[]{2, -3, 7, 8};
        int a2[] = new int[]{4, -6, 2, -1};
        int a3[] = new int[]{-6, 4, 7, 0};

        sort(a1);
        sort(a2);
        sort(a3);
     
        int sum;
     
        for(int i=0;i<a1.length;i++){
            int j=0,k=a3.length-1;
            while(j<a2.length && k>0){
               sum=a1[i]+a2[j]+a3[k];
               if(sum>0)
                   --k;
               else if(sum<0)
                   ++j;
               else if(sum==0)
                  System.out.println(a1[i]+"+"+a2[j]+"+"+a3[k]+"=0");
            }
            }
     
    }

    public static void sort(int[] a) {
        int j;
        boolean flag = true;
        int temp;

        while (flag) {
            flag = false;
            for (int i = 0; i < a.length - 1; i++) {
                if(a[i]>a[i+1]){
                temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                flag = true;
                }
            }
        }
    }
}

String Manipulation - Printing Sub Strings of a String in Incremental Order & Replacing '=' When StringIndexOutOfBoundsException Occurs


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package something;

import java.util.List;

/**
 *
 * @author Sunshine
 */
public class Something {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
           String input = "RaviKiran";  
     
        int index = 0;
        for (int i = 1;    ; i++) {                
            for (int j = 0; j < i; j++) {
                try {
                System.out.print(input.charAt(index));
                } catch (StringIndexOutOfBoundsException ex) {              
                    System.out.print("=");
                }
                index++;
            }
            System.out.println();
            if (index >= input.length()) {
                break;
            }
    }
}
}


OUTPUT


run:
R
av
iKi
ran=
BUILD SUCCESSFUL (total time: 0 seconds)


In place Negative and Positive separation Of Elements in an Array


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package separateposneg;

/**
 *
 * @author Sunshine
 */
public class SeparatePosNeg {

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {
        // TODO code application logic here
        int array[] = new int[]{2, -7, -6, -9, -5, 11, -19, 8, 14, 17};
        int neg,pos=0, temp;
        int n = array.length;
        for (int i = 0; i < n; i++)
        {
            if (array[i] > 0)
            {  
              //  System.out.println("--)))>"+i);
             
                temp=array[i];
                array[pos]=temp;
                //System.out.println("-ppp->"+pos);
                pos++;
                //System.out.println("-->"+pos);
                for(int k =i ; k >pos ;k--)
                {
                  //  System.out.println("-->"+i);
                 array[k]=array[k-1];
                }
             
             
            }
}

 for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);

        }

    }
}

Tuesday, 25 September 2012

Java Beginner : Strings and Arrays - Display words that start with a vowel in an user inserted string


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication7;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 *
 * @author Sunshine
 */
public class JavaApplication7 {

    /**

     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        System.out.println("Please enter some text");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String names=br.readLine().toLowerCase();
        StringTokenizer stk=new StringTokenizer(names);
        String vowels="";
        while(stk.hasMoreTokens()){
            String c=stk.nextToken();
            if(c.startsWith("a")||c.startsWith("e")||c.startsWith("i")||c.startsWith("o")||c.startsWith("u"))
                vowels=vowels+" "+c;
        }
        System.out.println(vowels);
        
    }
}