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);
       
       
    }
}