Template 3
1. Find sum of array elements,
Read 'n' array elements, and find sum of all elements using java program.
Code:
import java.util.Scanner;
class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the elements you want: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int sum = 0;
for(int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
System.out.println("Sum of array elements is : " + sum);
}
}
Output:
Enrollment No :22C21058 1 Name: Patel Dhruti
2. Sort an array in descending order
In this java program, we are reading total number of elements (N) first, and
then according the user input (value of N)/total number of elements, we are
reading the elements. Then sorting elements of array in descending order
and then printing the elements which are sorted in descending order.
Code:
import java.util.Scanner;
class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the total number of elements : ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < n - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
System.out.println("Sorted array in descending order:");
for (int i = 0; i < n; i++) {
System.out.println(arr[i] + "");
}
sc.close();
}
}
Output:
Enrollment No :22C21058 2 Name: Patel Dhruti
3. Sort an array in ascending order
In this java program, we are reading total number of elements (N) first, and
then according the user input (value of N)/total number of elements, we are
reading the elements. Then sorting elements of array in ascending order
and then printing the elements which are sorted in ascending order.
Code:
import java.util.Scanner;
class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the total number of elements : ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements: ");
for (int i = 0; i < n; i++) {
Enrollment No :22C21058 3 Name: Patel Dhruti
arr[i] = sc.nextInt();
}
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
System.out.println("Ascending order:");
for (int i = 0; i < n; i++) {
System.out.println(arr[i] + "");
}
sc.close();
}
}
Output:
4. Multiply two matrices
Enrollment No :22C21058 4 Name: Patel Dhruti
Given two matrices and find their multiplication in third matrix and print the
matrix using Java program.
Code:
import java.util.Scanner;
class Array
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base the matrices : ");
int n = sc.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
System.out.println("Enter the elements of 1st Matrix row wise");
for (int i = 0; i < n; i++) {
for (int j=0; j<n; j++){
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements of 2nd Matrix row wise");
for (int i = 0; i < n; i++) {
for (int j=0; j<n; j++){
b[i][j] = sc.nextInt();
}
}
int[][] result = new int[n][n];
for (int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
for(int k = 0; k<n; k++){
result[i][j] += a[i][k] * b[k][j];
}
}
}
System.out.println("Multiplying both the matrices...");
System.out.println("The product of the matrices is:");
for (int i = 0; i<n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(result[i][j] +" ");
}
System.out.println();
Enrollment No :22C21058 5 Name: Patel Dhruti
}
sc.close();
}
}
Output:
5. Subtract two matrices (subtraction of two matrices)
Given two matrices, we have to find and print their subtraction using Java
Program.
Code:
import java.util.Scanner;
class Array
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base the matrices : ");
int n = sc.nextInt();
int[][] a = new int[n][n];
Enrollment No :22C21058 6 Name: Patel Dhruti
int[][] b = new int[n][n];
System.out.println("Enter the elements of the first matrix :");
for (int i = 0; i < n; i++) {
for (int j=0; j<n; j++){
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements of the second matrix :");
for (int i = 0; i < n; i++) {
for (int j=0; j<n; j++){
b[i][j] = sc.nextInt();
}
}
int[][] result = new int[n][n];
for (int i = 0; i<n; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = a[i][j] - b[i][j];
}
}
System.out.println("Subtracting Matrices --Matrix1 - Matrix2--");
System.out.println("Result of Matrix1 - Matrix2 is:");
for (int i = 0; i<n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(result[i][j] +" ");
}
System.out.println();
}
sc.close();
}
}
Output:
Enrollment No :22C21058 7 Name: Patel Dhruti
6. Check sparse matrix
A matrix in which most of the elements are ‘0’ then it is said to be a sparse
matrix. Sparse matrices are used in specific ways in computer science and
have different storage and techniques related to their use.
Code:
import java.util.Scanner;
class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the dimension of the matrix : ");
int n = sc.nextInt();
int[][] a = new int[n][n];
//int[][] b = new int[n][n];
System.out.println("Enter the elements of the matrix :");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}
int count = 0;
int rows = a.length;
int cols = a[0].length;
int size = rows*cols;
Enrollment No :22C21058 8 Name: Patel Dhruti
for(int i =0;i < rows;i++){
for(int j=0; j<cols;j++){
if(a[i][j]==0)
count++;
}
}
if(count > (size/2))
System.out.println("The matrix is a sparse matrix");
else
System.out.println("The matrix is not a sparse matrix");
}
}
Output:
7. Find the common elements in two integer arrays
Given two integer arrays and we have to find common elements using java
program.
Code:
class Array
{
public static void main(String[] args)
{
int[] arr1 = {1993,1995,2000,2006,2017,2020};
int[] arr2 = {1990,1993,1995,2010,2016,2017};
System.out.print("Array1 : [");
for(int i=0;i<arr1.length-1;i++)
{
System.out.print(arr1[i]+",");
}
System.out.print(arr1[arr1.length-1]);
System.out.println("]");
Enrollment No :22C21058 9 Name: Patel Dhruti
System.out.print("Array2 : [");
for(int i=0;i<arr2.length-1;i++)
{
System.out.print(arr2[i]+",");
}
System.out.print(arr2[arr2.length-1]);
System.out.println("]");
for(int i=0;i<arr1.length;i++){
for(int j=0;j<arr2.length;j++){
if(arr1[i]==arr2[j]){
System.out.println("common element is: "+arr1[i]);
}
}
}
}
}
Output:
8. Find the common strings in two integer arrays
Given two integer arrays and we have to find common elements using java
program.
Code:
class Array
{
public static void main(String[] args)
{
int[] arr1 = {1993,1995,2000,2006,2017,2020};
int[] arr2 = {1990,1993,1995,2010,2016,2017};
System.out.print("Array1 : [");
for(int i=0;i<arr1.length-1;i++)
{
System.out.print(arr1[i]+",");
Enrollment No :22C21058 10 Name: Patel Dhruti
}
System.out.print(arr1[arr1.length-1]);
System.out.println("]");
System.out.print("Array2 : [");
for(int i=0;i<arr2.length-1;i++)
{
System.out.print(arr2[i]+",");
}
System.out.print(arr2[arr2.length-1]);
System.out.println("]");
for(int i=0;i<arr1.length;i++){
for(int j=0;j<arr2.length;j++){
if(arr1[i]==arr2[j]){
System.out.println("common element is: "+arr1[i]);
}
}
}
}
}
Output:
9. Find missing elements in array elements
Two string arrays are given and we have to find common strings using java
program.
Code:
class Array {
public static void main(String[] args) {
String[] array1 = {"C", "C++", "C", "JAVA", "SQL", "ORACLE"};
String[] array2 = {"Mys05", "SQL", "Android", "ORACLE", "PostgreSQL", "DEZ", "JAVA"};
System.out.println("Array1: ");
for (String element : array1) {
System.out.print(element + ", ");
Enrollment No :22C21058 11 Name: Patel Dhruti
}
System.out.println("\nArray2: ");
for (String element : array2) {
System.out.print(element + ", ");
}
System.out.print("\nCommon element(s): ");
for (String element1 : array1) {
for (String element2 : array2) {
if (element1.equals(element2)) {
System.out.print(element1 + ", ");
break;
}
}
}
}
}
Output:
10. Find average of all array elements
Given an array of integers(in a series) and we have to find its missing
elements using java program.
Code:
class Array{
static int findMissing(int arr[], int N)
{
int i;
int temp[] = new int[N+1];
for(i = 0; i <= N; i++) {
temp[i] = 0;
}
for(i = 0; i< N; i++){
temp[arr[i] - 1] = 1;
}
int ans = 0;
Enrollment No :22C21058 12 Name: Patel Dhruti
for(i = 0;i <= N; i++) {
if(temp[i] == 0)
ans = i+1;
}
System.out.print("Missing element is: ");
System.out.println(ans);
return ans;
}
}
class Driver{
public static void main(String[] args)
{
int arr[] = {1,2,3,4,6,7};
int n = arr.length;
Array.findMissing(arr, n);
}
}
Output:
11. Find differences between minimum and maximum numbers in an array.
Code:
import java.util.Scanner;
class Array{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter the elements: ");
for(int i = 0 ; i< n ; i++) {
arr[i] = sc.nextInt();
}
int sum = 0;
Enrollment No :22C21058 13 Name: Patel Dhruti
for (int num : arr) {
sum += num;
}
System.out.println("Sum of the array elements is: " + sum);
double avg = (double) sum/n;
System.out.println("Average of the array elements is: " + avg);
sc.close();
}
}
Output:
12. Move all zero at the end of the array
Given an array with zeros and we have to move all zeros at the end of the
array using java program.
Code:
public class Array
{
static void zeroatEnd(int arr[], int n) {
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] != 0) {
Enrollment No :22C21058 14 Name: Patel Dhruti
arr[count++] = arr[i];
}
}
while (count < n){
arr[count ++] = 0;
}
System.out.println("After moving 0 at the end");
System.out.print("Output array: ");
for(int i=0;i<n;i++){
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
int arr[] ={5,1,6,0,0,3,9,0,6,7,8,10,10,0,2};
int n = arr.length;
zeroatEnd(arr,n);
}
}
Output:
13. Delete a specific element from a one dimensional array
Given an array and an element to delete and we have to delete it from
array using java program.
Code:
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int size = scanner.nextInt();
int[] array = new int[size];
System.out.println("Enter the array elements:");
Enrollment No :22C21058 15 Name: Patel Dhruti
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}
System.out.print("Enter the element to delete: ");
int elementToDelete = scanner.nextInt();
System.out.print("Given array (elements will be read in program): ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i < array.length - 1) {
System.out.print(" ");
}
}
System.out.println();
System.out.println("Enter element to delete: " + elementToDelete);
// Find the index of the element to delete
int indexToDelete = -1;
for (int i = 0; i < array.length; i++) {
if (array[i] == elementToDelete) {
indexToDelete = i;
break;
}
}
if (indexToDelete != -1) {
// Shift elements to the left to fill the gap
for (int i = indexToDelete; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
// Resize the array to remove the last element
int[] newArray = new int[array.length - 1];
System.arraycopy(array, 0, newArray, 0, newArray.length);
array = newArray;
// Print the updated array
System.out.print("Array elements after deleting the element: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i < array.length - 1) {
System.out.print(" ");
}
}
} else {
System.out.println("Element not found in the array.");
Enrollment No :22C21058 16 Name: Patel Dhruti
}
scanner.close();
}
}
Output:
14. Print even and odd elements from an array.
Given a one dimensional array and we have to print its even and odd
elements separately.
Code:
class Array{
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int n = 5;
int arr[] = {10,11,12,13,14};
int evenSize = 0, oddSize = 0;
for(int i = 0; i < n;i++){
if(arr[i] % 2 == 0)
evenSize++;
else
oddSize++;
}
int[] even = new int[evenSize];
int[] odd = new int[oddSize];
Enrollment No :22C21058 17 Name: Patel Dhruti
int j=0, k=0;
for(int i=0; i<n; i++){
if(arr[i]%2 ==0)
even[j++] = arr[i];
else
odd[k++] = arr[i];
}
System.out.print("Odd numbers in the array are: ");
printArray(odd);
System.out.print("Even numbers in the array are: ");
printArray(even);
}
}
Output:
15. Merge two one dimensional arrays
Given two one-dimensional arrays and we have to merge them using
java program.
Code:
class Array {
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] array2 = {11, 12, 13, 14, 15};
int n1 = array1.length;
int n2 = array2.length;
int[] mergedArray = new int[n1 + n2];
// Copy elements from Array 1 to mergedArray
for (int i = 0; i < n1; i++) {
mergedArray[i] = array1[i];
}
// Copy elements from Array 2 to mergedArray
for (int i = 0; i < n2; i++) {
mergedArray[n1 + i] = array2[i];
}
System.out.println("New array (After merging elements):");
for (int num : mergedArray) {
System.out.print(num + " ");
Enrollment No :22C21058 18 Name: Patel Dhruti
}
}
}
Output:
Enrollment No :22C21058 19 Name: Patel Dhruti