Skip to main content

PAGE-4: PROGRAMS ON ARRAY


List of programs covered:-
  • List of programs covered:-
  • Program to add elements in an array using scanner and print the added elements
  • Program to find sum of each element in an Array
  • Program to find largest element in Array and its index
  • Program to find smallest element in Array and its index
  • Program to find smallest and largest element in array using Arrays.sort
  • Program to find smallest element in Array (using Integer function)
  • Program to remove element from array
  • Program to find common elements in 2 arrays
  • Program to find top 2 maximum numbers in an Array
  • Program to find Top 2 smallest number in an Array
  • Program to add string in array and print the added element
  • Program to find even and odd number in an array
  • Program for Matrix multiplication


Algorithm:
enter image description here
 What would you do on paper to solve Array programs :-
  1. Create and initialize the min value at tenIntArray[0]
  2. Create a variable to hold the index of the min value in the array and initialize it to 0 (because we said in 1. to initialize the min at tenIntArray[0])
  3. Loop through the elements of your array
  4. If you find an element inferior than the current min, update the minimum value with this element and update the index with the corresponding index of this element
  5. You're done



Program to add elements in an array using scanner and print the added elements
package com.array;

import java.util.Scanner;

public class TakingInputFromScanner {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter elements in an array");
int n = s.nextInt();
int arr[] = new int[n];
System.out.println("Size of an array is :" + arr.length);
for (int i = 0; i < n; i++) {// for reading array
arr[i] = s.nextInt();
}
for (int i : arr) { // for printing array
System.out.println("Elements in array are "+ i);
}
}
}

Output:
Enter elements in an array
3
Size of an array is :3
12
13
14
Elements in array are 12
Elements in array are 13
Elements in array are 14


Program to find sum of each element in an Array:
package com.array;

public class SumOfEachElementInArray {
public static void main(String[] args) {
int[] arr = new int[] { 101, 102, 103, 104, 105 };
double total = 0;
for (int i = 0; i < arr.length; i++) {
total += arr[i];
}
System.out.println("Total of each element in an array is  " + total);
}
}

Output:
Total of each element in an array is  515.0




Program to find largest element in Array and its index
package com.array;

public class IndexOfLargestElementsInArray {
public static void main(String[] args) {
int[] arr = new int[] { 101, 102, 103, 104, 105 };
int largest = arr[0], index = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] > largest) {
largest = arr[i];
index = i;
}

}
System.out.println("Largest element  :" + largest + " " + "and its Index is :" + " " + index);
}
}

Output:
Largest element  :105 and its Index is : 4


Program to find smallest element in Array and its index
package com.array;

public class SmallestElementInArrayWithoutInuiltFunction {
public static void main(String[] args) {
int[] inputArray = { 88, 33, 55, 23, 64, 123 };
int minValue = inputArray[0];
int index=0;
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] < minValue) {
minValue = inputArray[i];
index=i;
}
}
System.out.println("Smallest Element  : " +  minValue+ " " + "index :" +" " + index);
}
}

Output:
Smallest Element  : 23 index : 3


Program to find smallest and largest element in array using Arrays.sort
package com.array;

import java.util.Arrays;
public class SmallestElementInArray2 {
public static void main(String[] args) {
int[] inputArray = { 88, 33, 55, 23, 64, 123 };

// Find minimum value
Arrays.sort(inputArray);
System.out.println("Smallest value in array is: " + inputArray[0]);

// Find maximum value
Arrays.sort(inputArray);
System.out.println("Largest value in array is : " + inputArray[inputArray.length - 1]);
}

}

Output:
Smallest value in array is: 23
Largest value in array is : 123


Program to find smallest element in Array (using Integer function)
package com.array;

public class SmallestElementInArray {
public static void main(String[] args) {
int[] numbers = { 88, 33, 55, 23, 64, 123 };
int smallest = Integer.MAX_VALUE;
for (int i = 0; i < numbers.length; i++) {
if (smallest > numbers[i]) {
smallest = numbers[i];
}
}
System.out.println("Smallest number in array is : " + smallest);
}
}

Output:
Smallest number in array is : 23


Program to remove element from array

package com.array;

import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;

public class RemoveObjectFromArray {
public static void main(String[] args) {
int[] test = { 88, 33, 55, 23, 64, 123 };
System.out.println("Original Array : size : " + test.length);
System.out.println("Contents : " + Arrays.toString(test));
test = ArrayUtils.remove(test, 2); // removing element at index 2

// Size of array must be 1 less than original array after deleting an
// element
System.out.println("Size of array after removing an element  : " + test.length);
System.out.println("Content of Array after removing an object : " + Arrays.toString(test));

}
}

Output:
Original Array : size : 6
Contents : [88, 33, 55, 23, 64, 123]
Size of array after removing an element  : 5
Content of Array after removing an object : [88, 33, 23, 64, 123]

Program to find common elements in 2 arrays
package com.array;

public class CommonElementsInArray {
public static void main(String a[]) {
int[] arr1 = { 4, 7, 3, 9, 2 };
int[] arr2 = { 3, 2, 12, 9, 40, 32, 4 };
System.out.println("Common elements in both the arrays are : ");
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
System.out.println(arr1[i]);
}
}
}
}
}


Output:
Common elements in both the arrays are :
4
3
9
2


Program to find top 2 maximum numbers in an Array
package com.array;

public class TwoMaxNumbers {
public void printTwoMaxNumbers(int[] nums){
        int maxOne = 0;
        int maxTwo = 0;
        for(int n:nums){
            if(maxOne < n){
                maxTwo = maxOne;
                maxOne =n;
            } else if(maxTwo < n){
                maxTwo = n;
            }
        }
        System.out.println("First Max Number: "+maxOne);
        System.out.println("Second Max Number: "+maxTwo);
    }
     
    public static void main(String a[]){
        int num[] = {5,34,78,2,45,1,99,23};
        TwoMaxNumbers tmn = new TwoMaxNumbers();
        tmn.printTwoMaxNumbers(num);
    }
}

Output:
First Max Number: 99
Second Max Number: 78

Program to find Top 2 smallest number in an Array
package com.array;
public class Top2SmallestNumber {
public static void main(String args[]) {
int numbers[] = { 6, 3, 37, 12, 46, 5, 64, 21 };
int smallest = numbers[0];
int second_smallest = numbers[0];

for (int n : numbers) {
if (n < smallest) {
second_smallest = smallest;
smallest = n;
} else if (n < second_smallest) {
second_smallest = n;
}
}
System.out.println("First smallest Number: " + smallest);
System.out.println("Second Smallest Number: " + second_smallest);
}
}

Output:
First smallest Number: 3
Second Smallest Number: 5


Program to add string in array and print the added element
package com.array;

public class StringArrayExample {
public static void main(String[] args) {
String[] stringArray = new String[3];
stringArray[0] = "a";
stringArray[1] = "b";
stringArray[2] = "c";
System.out.println("String Array output");
for (int i = 0; i < stringArray.length; i++) {
System.out.println(stringArray[i]);
}
}
}

Output:
String Array output
a
b
c


Program to find even and odd number in an array
package com.array;

public class FindEvenOrOddNumber {
public static void main(String[] args) {
// create an array of 10 numbers
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i = 0; i < numbers.length; i++) {

/*
* use modulus operator to check if the number is even or odd. If we
* divide any number by 2 and reminder is 0 then the number is even,
* otherwise it is odd.
*/

if (numbers[i] % 2 == 0)
System.out.println(numbers[i] + " is even number.");
else
System.out.println(numbers[i] + " is odd number.");

}
}
}

Output:
1 is odd number.
2 is even number.
3 is odd number.
4 is even number.
5 is odd number.
6 is even number.
7 is odd number.
8 is even number.
9 is odd number.
10 is even number.


Program for Matrix multiplication




package com.array;

import java.util.Scanner;

public class MatrixMultiplication {
public static void main(String args[]) {

int i, j, k, cols, rows, p;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows: ");
rows = s.nextInt();
System.out.print("Enter number of cols: ");
cols = s.nextInt();
int a[][] = new int[rows][cols]; // First Array Elements
int b[][] = new int[rows][cols]; // Second Array Elements
int c[][] = new int[rows][cols]; // Multiplication of Two Matrix
p = rows;
System.out.println("Enter the first matrix");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println("Enter the second matrix");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
b[i][j] = s.nextInt();
}
}
// Multiplication start from here
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
c[i][j] = 0;
for (k = 0; k < p; k++) {
c[i][j] = a[i][k] * b[k][j] + c[i][j];
}
}
}

// just for user display
System.out.println("First Matrix is: ");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}

// just for user display
System.out.println("Second Matrix is: ");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}

// Print the multiplication result here
System.out.println("Multiplication matrix is: ");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}

} // end method main
} // end class


Output:
Enter number of rows: 2
Enter number of cols: 2
Enter the first matrix
2
3
4
2
Enter the second matrix
2
3
4
2
First Matrix is: 
16 12 
16 16 
Second Matrix is: 
16 12 
16 16 
Multiplication matrix is: 
16 12 
16 16 


















Comments