Saturday, 10 September 2011

Array :: Entering values through key board in java

    import java.util.*;
    public class Array
    {
        public static void main(String args[])
        {
            int[] a=new int[6];
            Scanner sc=new Scanner(System.in);
            System.out.println("Please enter elements...");
            for(int j=0;j<6;j++)
                a[j]=sc.nextInt();
            System.out.println("Array elements are : ");
            for (int i=0;i<a.length;i++)
                System.out.println(a[i]);
        }
    }

Sorting Array in Numbers in Java

import java.util.*;

public class  ArrayDemo{
  public static void main(String[] args){
  int num[] = {50,20,45,82,25,63};
  int l = num.length;
  int i,j,t;
  System.out.print("Given number : ");
  for (i = 0;i < l;i++ ){
  System.out.print("  " + num[i]);
  }
  System.out.println("\n");
  System.out.print("Accending order number : ");
  Arrays.sort(num);
    for(i = 0;i < l;i++){
  System.out.print("  " + num[i]);
  }
  }
}

public class CopyArray {
    public static void main(String[] args) {
        int array1[]= {2,3,4,5,8,9};
        int array2[] = new int[6];
        System.out.println("array:");
        System.out.print("[");
        for (int i=0; i<array1.length; i++){
            System.out.print(" "+array1[i]);
        }
        System.out.print("]");
        System.out.println("\narray1:");
        System.out.print("[");
        for(int j=0; j<array1.length; j++){
            array2[j] = array1[j];
            System.out.print(" "+ array2[j]);
        }
        System.out.print("]");
    }
}

public class Sum
{
  public static void main(String[] args)
  {
  int[] x = new int [101];
  for (int i = 0; i<x.length; i++ )
  x[i] = i;
  int sum = 0;
  for(int i = 0; i<x.length; i++)
  sum += x[i];
  System.out.println(sum);
  }
}

output:
5050