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

Array

The document provides an overview of arrays in Java, detailing their characteristics, such as being a non-primitive data type and fixed in size. It includes multiple Java programming exercises that demonstrate how to create, manipulate, and analyze arrays, such as finding even and odd elements, calculating sums and averages, and searching for specific elements. Additionally, it covers advanced topics like swapping elements and reversing arrays.

Uploaded by

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

Array

The document provides an overview of arrays in Java, detailing their characteristics, such as being a non-primitive data type and fixed in size. It includes multiple Java programming exercises that demonstrate how to create, manipulate, and analyze arrays, such as finding even and odd elements, calculating sums and averages, and searching for specific elements. Additionally, it covers advanced topics like swapping elements and reversing arrays.

Uploaded by

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

* Array:

1. Array is a predefined class present in java.lang package.

2. It is considered as a non-primitive datatype in java.

3. Array is a continuous block of memory which is used to store multiple values or data.

4. It is a collection of homogeneous elements.

5. Array is fixed size in nature.

6. whenever we create an array object assigning datatype and passing size is mandatory.

7. By default, in array object the default value of datatype is stored.

8. To store or access an element from an array we need array reference variable and index value.

* Array reference variable:

A variable which stores the address of an array object is known as Array reference variable.

* Index value:

It is a positive integer number which starts with 0.

NOTE: To store multiple values, we need to create array object.

* We can create array object in 2 ways:

1. Using new keyword

2. Without using new keyword

Q1. Write a java program to store the elements in an array and access the elements one by one.

(Note: make use of scanner class to request the data from end user)

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

//The loop is used to store the elements in array.

for(int index = 0; index<arr.length; index++)

{
arr[index] = sc.nextInt();

//The loop is used to access elements from an array

System.out.println("The created array is: ");

for(int index = 0; index < arr.length; index++)

System.out.println(arr[index]);

sc.close();

Q2. Write a java program to access the even elements in array.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

System.out.println("The even elements in array are: ");

for(int index = 0; index < arr.length; index++)

if(arr[index] % 2 == 0)

System.out.println(arr[index]);

sc.close();}

Q3. Write a java program to access the odd elements in array.

 public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

System.out.println("The odd elements in array are: ");

for(int index = 0; index < arr.length; index++)

if(arr[index] % 2 != 0)

System.out.println(arr[index]);

sc.close();}

Q4. Write a java program to identify the sum of elements present in array.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

int sum = 0;
for(int index = 0; index < arr.length; index++)

sum = arr[index] + sum;

System.out.println("The sum of array elements is: " + sum);

sc.close();}

Q5. Write a java program to identify the average if elements in an array.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

int sum = 0;

int avg = 0;

for(int index = 0; index < arr.length; index++)

sum = arr[index] + sum;

avg = sum / arr.length;

System.out.println("The average of array elements is: " + avg);

sc.close();}

Q6. Write a java program to identify the average of odd elements from an array.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();


int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

int sum = 0;

int count = 0;

for(int index = 0; index < arr.length; index++)

if(arr[index] % 2 != 0) {

sum = arr[index] + sum;

count++;

System.out.println("The average of array elements is: " + sum/count);

sc.close();}

Q7. Write a java program to identify the sum of even elements and odd elements.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

System.out.println("The even elements in array are: ");

int sumeven = 0;

int sumodd = 0;

for(int index = 0; index < arr.length; index++)


{

if(arr[index] % 2 == 0)

sumeven = arr[index] + sumeven;

else

sumodd = arr[index] + sumodd;

System.out.println("The sum of even array elements is: " + sumeven);

System.out.println("The sum of odd array elements is: " + sumodd);

sc.close();}

Q8. Write a java program to print the elements which is present in the even index.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

System.out.println("The elements which is present in the even index are: ");

for(int i=0; i<arr.length; i++)

if(i % 2 == 0)

System.out.println(arr[i]);

}
sc.close();}

Q9. WAJP to print the elements which is divisible by 5 from an array.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

System.out.println("The elements which is divisible by 5 are: ");

for(int i=0; i<arr.length; i++)

if(arr[i] % 5 == 0)

System.out.println(arr[i]);

sc.close();}

Q10. WAJP to search an element in an array. (if the given element is present in the array or not)

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)


{

arr[index] = sc.nextInt();

System.out.println("Enter the element to search: ");

int element = sc.nextInt();

int count = 0;

for(int i=0; i<arr.length; i++)

if(arr[i] == element) {

count++;

System.out.println(element + " is present in the array at " + i + " index.");

break;

if(count == 0)

System.out.println(element + " is not present in the array.");

sc.close();}

Q11. WAJP to identify/ count the no. of +ve and -ve elements in the array.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

//System.out.println("Enter the element to search: ");

//int element = sc.nextInt();

int countPositive = 0;

int countNegative = 0;
for(int i=0; i<arr.length; i++)

if(arr[i]<0)

countNegative++;

else

countPositive++;

System.out.println("No. of +ve elements present in the array are " + countPositive);

System.out.println("No. of -ve elements present in the array are " + countNegative);

sc.close(); }

IMP Question: WAJP to swap the numbers without using extra variable.

 public static void main(String[] args) {

int a = 10;

int b = 20;

System.out.println("Numbers before swapping are: " + a + ", " + b);

a = a+b;

b = a-b;

a = a-b;

System.out.println("Numbers after swapping are: " + a + ", " + b);

IMP Question: WAJP to swap the numbers.

 public static void main(String[] args) {

int a = 10;

int b = 20;

int temp = a;

System.out.println("Numbers before swapping are: " + a + ", " + b);

a = b;

b = temp;

System.out.println("Numbers after swapping are: " + a + ", " + b);

Q12. WAJP to reverse the given array.


 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

System.out.println("Elements before reverse are: ");

for(int i = 0; i<arr.length; i++)

System.out.println(arr[i]);

System.out.println("Elements After Reversing are : ");

int start = 0;

int end = arr.length-1;

while(start<end)

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

start++;

end--;

for(int i = 0; i<arr.length; i++)

System.out.println(arr[i]);

sc.close();

}
Q13. WAJP to find the smallest element in array.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

System.out.println("Elements in array are: ");

int smallest = arr[0];

for(int i = 1; i<arr.length; i++)

if(smallest > arr[i])

smallest = arr[i];

System.out.println("The "+ smallest + " is the smallest element.");

sc.close();}

Q14. WAJP to find the largest element in array.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

}
System.out.println("Elements in array are: ");

int largest = arr[0];

for(int i = 1; i<arr.length; i++)

if(largest < arr[i])

largest = arr[i];

System.out.println("The "+ largest + " is the largest element.");

sc.close(); }

Q15. WAJP to find the highest sum of two elements in an array.

 public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of an array: ");

int size = sc.nextInt();

int[] arr = new int[size];

System.out.println("The size of array is: " + arr.length);

System.out.println("Enter the elements in array: ");

for(int index = 0; index<arr.length; index++)

arr[index] = sc.nextInt();

System.out.println("Elements in array are: ");

int sum = arr[0];

for(int i = 0; i<arr.length; i++)

for(int j = i+1; j<arr.length; j++)

if(sum < arr[i]+arr[j])

sum = arr[i]+arr[j];

System.out.println("The highest sum of two elements is: " + sum);

sc.close();}
Q16. WAJP to find out the 8.

Given number = 5

Elements inserted in array = 1,2,3,4,5,6

1+2!=5 1+3!=5 1+4=5(1,4) and so on

Q17. WAJP to reverse each element in an array

Elements inserted in array = 12,23,45,56

Answer => 21,32,54,65

Q18. WAJP to find out the second largest element from an array.

You might also like