Tuesday 29 March 2016

Published 23:04:00 by with 0 comment

Pattern program in java

public class Triangle {

    public static void main(String[] args) {

        int i, j, t = 0;
        int k;
        for (i = 0; i <= 4; i++) {

            for (j = t; j <= 4; j++) {

                System.out.print(" ");
            }
            for (k = 0; k < i; k++) {
                System.out.print(" * ");
                t = t + 1;
            }
            System.out.println(" ");

        }
    }
}

OUTPUT:-

                 *
             *      *
         *      *     *
     *     *     *      *
Read More
      edit

Sunday 20 March 2016

Published 22:48:00 by with 0 comment

Write a program to Reverse a Array Elements in java

public class Reverse {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Size of an Array :");
        int n = sc.nextInt();
        int arr[] = new int[n];
        System.out.println("Enter Array elements  :");
        for (int i = 0; i < n; i++) {

            arr[i] = sc.nextInt();

        }
        for (int i = n-1; i >= 0; i--) {

            System.out.println("Reverse elements are :" + arr[i]);

        }

    }

}


Read More
      edit
Published 22:33:00 by with 0 comment

Write a program to Linear Search in java

public class LinearSearch {

    public static void main(String[] args) {

        int n;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Size of an Array  :");
        n=sc.nextInt();
        int arr[] = new int[n];
        int choice;
        int flag = 0;
     
        for (int i = 0; i < n; i++) {
            System.out.print("Enter " + i + " index value :");
            arr[i] = sc.nextInt();

        }
        System.out.print("Enter your Number you want to search :");
        choice = sc.nextInt();
        for (int i = 0; i < n; i++) {

            if (choice == arr[i]) {

                System.out.print("location " + i);
                flag = 1;
            }

        }
        if (flag == 1) {

            System.out.println("  Number found.");
        } else {
            System.out.println("Number not found.");
        }
    }

}



Read More
      edit