Friday, 14 September 2012

Java program codes: 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

No comments:

Post a Comment