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