# Java Menu Driven Program to Perform Array Operation

Array in Java is called as a non primitive data type which stores a fixed number of single type values. It is called a One-dimensional array.

In this article, we will see how to perform different array operations like checking duplicate elements, printing array in reverse order, checking largest element, checking smallest element, finding sum of all array elements by using Java Menu driven program. We will be implementing the application using a switch case.

## Instance-1

Suppose we have created an array containing 6 elements and array elements are [2,4,6,2,6,8].
Now we will find duplicate elements of an array. And hence result will be Duplicate elements in given array:
2
6


## Instance-2

Suppose we have created an array containing 6 elements and array elements are[2,4,6,2,6,8].
Now we will print the array in reverse order. And hence result will be
Original array:
2 4 6 2 6 8
Array in reverse order:
8 6 2 6 4 2


## Instance-3

Suppose we have created an array containing 6 elements and array elements are [2,4,6,2,6,8]. Now we will print the largest element in an array.
And hence result will be Largest element present in given array: 8


## Instance-4

Suppose we have created an array containing 6 elements and array elements are [2,4,6,2,6,8]. Now we will print the smallest element in an array. And hence result will be.
Smallest element present in given array: 2


## Instance-5

Suppose we have created an array containing 6 elements and array elements are [2,4,6,2,6,8]. Now we will print the sum of all the items of the array. And hence result will be
Sum of all the elements of an array: 28


## Syntax

To perform basic operations in an array like finding duplicates, reverse of an array, largest element of an array, smallest element of an array, printing sum of all elements of an array we use a for loop with some basic logic for each part mentioned above.

Following is the syntax for “for loop” −

for(initialization; condition; increment/decrement){//statement}


## Algorithm

Step-1 − Ask the user to input the desired element to make an array.

Step-3 − Ask the user to enter their choice.

Step-4 − Use a switch case to go to the choice and perform the operation.

Step-5 − Print the result.

Let’s see the program to understand it clearly.

## Example

import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
int n=sc.nextInt();
int[] array = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++) {
array[i]=sc.nextInt();
}
System.out.println("Array elements are: ");
for (int i=0; i<n; i++) {
System.out.println(array[i]);
}
mainLoop: while (true) {
Scanner inn = new Scanner( System.in );
System.out.println("1. Find duplicate elements of an array");
System.out.println("2. Print array in reverse order");
System.out.println("3. Print the largest element in an array");
System.out.println("4. Print the smallest element in an array");
System.out.println("5. Print the sum of all the items of the array");
System.out.println("6. Terminate the program");
System.out.println("Enter action number (1-6): ");
int command;
if ( inn.hasNextInt() ) {
command = inn.nextInt();
inn.nextLine();
}
else {
System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER.");
inn.nextLine();
continue;
}
switch(command) {
case 1:
System.out.println("Duplicate elements in given array: ");
for(int i = 0; i < array.length; i++) {
for(int j = i + 1; j < array.length; j++)
if(array[i] == array[j])
System.out.println(array[j]);
}
}
break;
case 2:
System.out.println("Original array: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("Array in reverse order: ");
for (int i = array.length-1; i >= 0; i--) {
System.out.print(array[i] + " ");
}
break;
case 3:
int max = array;
for (int i = 0; i < array.length; i++) {
if(array[i] > max)
max = array[i];
}
System.out.println("Largest element present in given array: " + max);
break;
case 4:
int min = array;
for (int i = 0; i < array.length; i++) {
if(array[i] <min)
min = array[i];
}
System.out.println("Smallest element present in given array: " + min);
break;
case 5:
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum = sum + array[i];
}
System.out.println("Sum of all the elements of an array: " + sum);
break;
case 6:
System.out.println("Program terminated");
break mainLoop;
default:
System.out.println("Wrong choice!!");

}
}
}
}


## Output

Enter the number of elements you want to store: 5
Enter the elements of the array:
4 1 5 3 2
Array elements are:
4
1
5
3
2

1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
2
Original array:
4 1 5 3 2
Array in reverse order:
2 3 5 1 4

1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
1
Duplicate elements in given array:

1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
5
Sum of all the elements of an array: 15

1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
3
Largest element present in given array: 5

1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
4
Smallest element present in given array: 1

1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
6
Program terminated


In this article, we explored how to perform different array operations in Java by using menu driven approach.