0% found this document useful (0 votes)
119 views

Exercise 1. Write A Java Program To Print The Elements of An Array Present On Even Position. Solution

The document provides 8 solutions to common array problems in Java. Each solution includes code to: 1) Define and initialize an array 2) Perform operations like sorting, filtering, or copying elements 3) Print the original and/or resulting array to demonstrate the solution The problems addressed include: printing even/odd elements, sorting in ascending/descending order, checking for equal matrices, reversing/copying arrays, and finding duplicate elements. For each problem, efficient code snippets are provided to manipulate and process array data as required.

Uploaded by

Hana hana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

Exercise 1. Write A Java Program To Print The Elements of An Array Present On Even Position. Solution

The document provides 8 solutions to common array problems in Java. Each solution includes code to: 1) Define and initialize an array 2) Perform operations like sorting, filtering, or copying elements 3) Print the original and/or resulting array to demonstrate the solution The problems addressed include: printing even/odd elements, sorting in ascending/descending order, checking for equal matrices, reversing/copying arrays, and finding duplicate elements. For each problem, efficient code snippets are provided to manipulate and process array data as required.

Uploaded by

Hana hana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Exercise

1. Write a java program to print the elements of an array present on even position.

Solution
package labTest;
import java.util.Scanner;
public class EvenPosition
{
public static void main(String[] args)
{
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);
System.out.println("Enter an array size : ");
int size = scan.nextInt();
System.out.print("Enter Array Elements : ");
for(int i=0; i<size; i++)
{
arr[i] = scan.nextInt();
}

System.out.println("Elements of an array : ");


for(int i=0; i<size; i++)
{
System.out.print(arr[i] + "\t");
}

System.out.println("\nElements of an array present on even position: ");


for(int i=0; i<size; i+=2)
{
System.out.print(arr[i] + "\t");
}
scan.close();
}
}
(OR)
package labTest;
public class EvenPosition_1
{
public static void main(String[] args)
{
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};

System.out.println("Elements of given array present on even position: ");


//Loop through the array by incrementing value of i by 2
//Here, i will start from 1 as first even positioned element is present
at position 1.
for (int i = 1; i < arr.length; i = i+2)
{
System.out.println(arr[i]);
}
}
}
2. Write a java program to sort the elements of an array in ascending order.

Solution
package Exercise;
public class SortAsc
{
public static void main(String[] args)
{
//Initialize array
int [] arr = {5, 2, 8, 7, 1};
int temp = 0;
//Displaying elements of original array
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
//Sort the array in ascending order
for (int i = 0; i < arr.length; i++)
{
for (int j = i+1; j < arr.length; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
//Displaying elements of array after sorting
System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}

3. Write a java program to sort the elements of an array in descending order.


Solution
package Exercise;
public class SortDsc
{
public static void main(String[] args)
{
//Initialize array
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
//Displaying elements of original array
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
//Sort the array in descending order
for (int i = 0; i < arr.length; i++)
{
for (int j = i+1; j < arr.length; j++)
{
if(arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
//Displaying elements of array after sorting
System.out.println("Elements of array sorted in descending order: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}

4. Write a java program to print Odd and Even Numbers from an Array.
Solution

package Exercise;
public class OddEvenInArrayExample
{
public static void main(String[] args)
{
int a[]={1,2,5,6,3,2};
System.out.println("Odd Numbers:");
for(int i=0;i<a.length;i++)
{
if(a[i]%2!=0)
{
System.out.println(a[i]);
}
}
System.out.println("Even Numbers:");
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
System.out.println(a[i]);
}
}
}
}
5. Write a Java Program to determine whether two matrices are equal.
Solution
package Exercise;
public class EqualMatrix
{
public static void main(String[] args)
{
int row1, col1, row2, col2;
boolean flag = true;
//Initialize matrix a
int a[][] = { {1, 2, 3},
{8, 4, 6},
{4, 5, 7}
};
//Initialize matrix b
int b[][] = { {1, 2, 3},
{8, 4, 6},
{4, 5, 7}
};
//Calculates the number of rows and columns present in the first matrix
row1 = a.length;
col1 = a[0].length;
//Calculates the number of rows and columns present in the second matrix
row2 = b.length;
col2 = b[0].length;
//Checks if dimensions of both the matrices are equal
if(row1 != row2 || col1 != col2)
{
System.out.println("Matrices are not equal");
}
else
{
for(int i = 0; i < row1; i++)
{
for(int j = 0; j < col1; j++)
{
if(a[i][j] != b[i][j])
{
flag = false;
break;
}
}
}
if(flag)
System.out.println("Matrices are equal");
else
System.out.println("Matrices are not equal");
}
}
}

6. Write a java program to print the elements of an array in reverse order.


Solution
package Exercise;
public class ReverseArray
{
public static void main(String[] args)
{
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
System.out.println("Original array: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}

System.out.println();
System.out.println("Array in reverse order: ");

//Loop through the array in reverse order


for (int i = arr.length-1; i >= 0; i--)
{
System.out.print(arr[i] + " ");
}
}
}
7. Write a java program to print the duplicate elements of an array.

Solution

package Exercise;
public class DuplicateElement
{
public static void main(String[] args)
{
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3};
System.out.println("Duplicate elements in given array: ");
//Searches for duplicate element
for(int i = 0; i < arr.length; i++)
{
for(int j = i + 1; j < arr.length; j++)
{
if(arr[i] == arr[j])
System.out.println(arr[j]);
}
}
}
}
8. Write a java program to copy all elements of one array into another array.

Solution

package Exercise;
public class CopyArray
{
public static void main(String[] args)
{
//Initialize array
int [] arr1 = new int [] {1, 2, 3, 4, 5};

//Create another array arr2 with size of arr1


int arr2[] = new int[arr1.length];

//Copying all elements of one array into another


for (int i = 0; i < arr1.length; i++)
{
arr2[i] = arr1[i];
}

//Displaying elements of array arr1


System.out.println("Elements of original array: ");
for (int i = 0; i < arr1.length; i++)
{
System.out.print(arr1[i] + " ");
}

System.out.println();

//Displaying elements of array arr2


System.out.println("Elements of new array: ");
for (int i = 0; i < arr2.length; i++)
{
System.out.print(arr2[i] + " ");
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy