List of Java Array Programs

1.Write a program to find largest element in the array.

In the below code we will write the logic of finding the largest element in the array.

suppose input is given 2 ,4, 9, 10 .Output would be 10.

public class LargestElement
{
public static void main(String[] arg)
{
int []array=new int[]{2,4,9,10};//initialize the array here
int max=array[0];//setting the maximum value at 0 index
for(int i=1;i<array.lenght;i++)
{
if(array[i]>max)//comparing the array element with max
{
max=array[i];
}
System.out.println("Largest element in the array" +max);
}
}
}

Output:

Largest element in the array 10

Explanation:

step 1: array element is given 2 4 9 10 
step 2:set max=2(0 index of array)
step 3:iterate the loop i=1 to i<array.lenght or 4.
step 4:if(array[1]>max) or (4>2) means max=4
       if(array[2]>max) or (9>4) means max=9
       if(array[3]>max) or (10>9) means max=10
step 5:exit from the loop
step 6:print the max value

2.Write a program to find smallest element in the array.

In the below code we will write the logic of finding the smallest element in the array.

Suppose input is given 2 ,4, 9, 10.Output would be 2.

public class SmallestElement
{
public static void main(String[] arg)
{
int []array=new int[]{2,4,9,10};//initialize the array here
int min=array[0];//setting the maximum value at 0 index
for(int i=1;i<array.lenght;i++)
{
if(array[i]<min)//comparing the array element with max
{
max=array[i];
}
System.out.println("Smallest element in the array" +min);
}
}
}

Output:

Smallest element in the array 2

3 Write a program of sum of five number using array

Suppose given five number 1,2,3,4,5 then output would be 15.

public class SumProgram
{
public static void main(String[] arg)
{
int []array=new int[]{1,2,3,4,5};
int sum=0;
for(int i=0;i<array.lenght;i++)
{
sum=sum+array[i];//summing the array element
}
System.out.println ("sum of five number" +sum);
}
}

Output:

sum of five number 15

4 Write a program of the array element in reverse order.

suppose input is given 2 4 6 8. Output would be 8 6 4 2.

public class ReverseArrayElement
{
public static void main(String[] arg)
{
int []array=new int[]{2,4,6,8};
System.out.println("Array element");
for(int i=0;i<array.lenght;i++)
{
System.out.print(array[i]+" ");
}
System.out.println();
System.out.println("Reverse array element");
for(int k=array.lenght-1;k>=0;k--)
{
System.out.print(array[k]+" ")
}
}
}

Output:

Array element
2 4 6 8
Reverse array element
8 6 4 2 

5 Write a program to print Even &Odd numbers in Array

suppose input is given 2 7 3 8 9 10.Output would be

even number=2 8 10 .Odd number=7 3 9

import java.util.Scanner;
public class EvenOddElement
{
public static void main(String[] arg)
{
int array[],n;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of element"); 
n=in.nextInt();
System.out.println("Enter all integer element");
for(int i=0;i<n;i++)
{
array[i]=in.nextInt();
}
System.out.print("Even number:");
for(int i=0;i<n;i++)
{
if(array[i]%2==0)
{
System.out.print(array[i]);
}
}
System.out.print("Odd number:");
for(int k=0;k<n;i++)
{
if(array[k]%2!=0)
{
System.out.print(array[k]+" ")
}
}
}
}

Output:

Enter the number of element
6
Enter all integer element
2 
7
3
8
9
10
Even number:2 8 10
Odd number:7 3 9

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *