1. WAP to find out sum of array elements.
import [Link].*;
public class arrays_sum {
public static void main(String args[]) {
int sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter the no of terms:");
int n = [Link]();
int num[] = new int[n];
[Link]("Enter the array elements:");
for (int i = 0; i < n; i++) {
num[i] = [Link]();
sum = sum + num[i];
}
[Link]("The total sum is: " + sum);
[Link]();
}
}
[Link] to find even/odd of array elements.
import [Link].*;
public class arrays {
public static void main(String args[]) {
int i;
Scanner sc = new Scanner([Link]);
[Link]("Enter the size");
int size = [Link]();
int num[] = new int[size];
[Link]("Enter the values:");
for (i = 0; i < size; i++) {
num[i] = [Link]();
}
for (i = 0; i < [Link]; i++) {
if (num[i]%2!=0) {
[Link]("Even");
}
else {
[Link]("Odd");
}
}
[Link] to find out sum of two 1D elements.
class AddTwoArray {
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4, 5 };
int b[] = { 6, 7, 8, 9, 10 };
int c[] = new int[5];
for (int i = 0; i < 5; ++i) {
c[i] = a[i] + b[i];
[Link]("Enter sum of " + i +
"index" + " " + c[i]);
}
}
}
[Link] to multiply two 1D array.
class AddTwoArray {
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4, 5 };
int b[] = { 6, 7, 8, 9, 10 };
int c[] = new int[5];
for (int i = 0; i < 5; ++i) {
c[i] = a[i] * b[i];
[Link]("Enter sum of " + i + "index" + " " + c[i]);
}
}
}
5. WAP to find key elements in array/binary
searching.
import [Link].*;
class Binary_Searching {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the no of terms:");
int size = [Link]();
int num[] = new int[size];
[Link]("Enter the values");
for (int i = 0; i < size; i++) {
num[i] = [Link]();
}
[Link]("Enter the key or value you want to search:");
int x = [Link]();
[Link]();
int first = 0;
int last = [Link] - 1;
int mid = (first + last) / 2;
while (first <= last) {
if (num[mid] < x) {
first = mid + 1;
} else if (num[mid] == x) {
[Link]("Element is found at " + mid);
break;
} else {
last = mid - 1;
}
mid = (first + last) / 2;
}
if (first > last) {
[Link]("Element is not present");
}
}
}
[Link] to find the ascending order in an array.
import [Link];
public class ascendingorder_array
{
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of elements you want in array:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = [Link]();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
[Link]("Ascending Order:");
for (int i = 0; i < n - 1; i++) {
[Link](a[i] + ",");
}
[Link](a[n - 1]);
}
}
[Link] to find the descending order in array.
import [Link];
public class ascendingorder_array {
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of elements you want in array:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = [Link]();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
[Link]("Descending Order:");
for (int i = 0; i < n; i++) {
[Link](a[i] + ",");
}
}
}
[Link] to read and display 2D array.
import [Link].*;
public class arrays_2D {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the no of rows:");
int r = [Link]();
[Link]("Enter the no of columns:");
int c = [Link]();
int num[][] = new int[r][c];
[Link]();
[Link]("Enter the " + (r * c) + " elements:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
num[i][j] = [Link]();
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link](num[i][j]+ " ");
}
[Link]();
}
}
}
[Link] that creates jagged arrays and display.
public class jagged_array {
public static void main(String[] args) {
int[][] jA = { { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8, 9 } };
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < jA[i].length; j++) {
[Link](jA[i][j] + " ");
}
[Link]();
}
}
}