Array
Array
3. Array is a continuous block of memory which is used to store multiple values or data.
6. whenever we create an array object assigning datatype and passing size is mandatory.
8. To store or access an element from an array we need array reference variable and index value.
A variable which stores the address of an array object is known as Array reference variable.
* Index value:
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)
{
arr[index] = sc.nextInt();
System.out.println(arr[index]);
sc.close();
arr[index] = sc.nextInt();
if(arr[index] % 2 == 0)
System.out.println(arr[index]);
sc.close();}
arr[index] = sc.nextInt();
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.
arr[index] = sc.nextInt();
int sum = 0;
for(int index = 0; index < arr.length; index++)
sc.close();}
arr[index] = sc.nextInt();
int sum = 0;
int avg = 0;
sc.close();}
Q6. Write a java program to identify the average of odd elements from an array.
arr[index] = sc.nextInt();
int sum = 0;
int count = 0;
if(arr[index] % 2 != 0) {
count++;
sc.close();}
Q7. Write a java program to identify the sum of even elements and odd elements.
arr[index] = sc.nextInt();
int sumeven = 0;
int sumodd = 0;
if(arr[index] % 2 == 0)
else
sc.close();}
Q8. Write a java program to print the elements which is present in the even index.
arr[index] = sc.nextInt();
if(i % 2 == 0)
System.out.println(arr[i]);
}
sc.close();}
arr[index] = sc.nextInt();
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)
arr[index] = sc.nextInt();
int count = 0;
if(arr[i] == element) {
count++;
break;
if(count == 0)
sc.close();}
Q11. WAJP to identify/ count the no. of +ve and -ve elements in the array.
arr[index] = sc.nextInt();
int countPositive = 0;
int countNegative = 0;
for(int i=0; i<arr.length; i++)
if(arr[i]<0)
countNegative++;
else
countPositive++;
sc.close(); }
IMP Question: WAJP to swap the numbers without using extra variable.
int a = 10;
int b = 20;
a = a+b;
b = a-b;
a = a-b;
int a = 10;
int b = 20;
int temp = a;
a = b;
b = temp;
arr[index] = sc.nextInt();
System.out.println(arr[i]);
int start = 0;
while(start<end)
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
System.out.println(arr[i]);
sc.close();
}
Q13. WAJP to find the smallest element in array.
arr[index] = sc.nextInt();
smallest = arr[i];
sc.close();}
arr[index] = sc.nextInt();
}
System.out.println("Elements in array are: ");
largest = arr[i];
sc.close(); }
arr[index] = sc.nextInt();
sum = arr[i]+arr[j];
sc.close();}
Q16. WAJP to find out the 8.
Given number = 5
Q18. WAJP to find out the second largest element from an array.