Java Array Progam

Largest.java

class Largest {
 public static void main(String args[]) {
  int a[] = {
   3,
   5,
   6,
   2,
   4
  };
  int i, large;
  large = a[0];
  for (i = 1; i <= 4; i++) {
   if (a[i] > large)
    large = a[i];
  }
  System.out.println("the largest item is:" + large);
 }
}

Output

the largest item is:6

Sort.java

import java.util.Scanner;
class Sort {
    public static void main(String args[]) {
        int i, j, temp, size;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the size of the array");
        size = s.nextInt();
        int a[] = new int[size];
        System.out.println("Enter the array elements");
        for (i = 0; i < size; i++) {
            a[i] = s.nextInt();
        }
        for (i = 0; i < size; i++) {
            for (j = 0; j < size - i - 1; j++) {
                if (a[j] > a[j + 1]) {
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        System.out.println("The array elements are in sorted order");
        for (i = 0; i < size; i++) {
            System.out.println(a[i]);
        }
    }
}

Output:

Enter the size of the array
5
Enter the array elements
3
6
1
9
7
The array elements are in sorted order
1
3
6
7
9

Search.java

import java.util.Scanner;
class Search
{
public static void main(String args[])
{
int i,item,loc=-1,size;
Scanner s= new Scanner(System.in);
System.out.println("Enter the size of the array");
size=s.nextInt();
int a[]=new int[size];
System.out.println("Enter the array elements");
for(i=0;i<size;i++)
{
a[i]=s.nextInt();
}
System.out.println("Enter the item to be searched");
item=s.nextInt();
for(i=0;i<size;i++)
{
if(item==a[i])
{
loc=i;
break;
}
}
System.out.println("index location is"+loc);
}
}

Output:

Enter the size of the array
5
Enter the array elements
2
5
3
7
9
Enter the item to be searched
5
Index location is 1.

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment