SOLUTION 01
import java.util.Scanner;
class Test
{
public static void main(String []args)
{
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the array");
int length = S.nextInt();
int [] array = new int[length];
for(int i=0 ;i<length ; i++)
array[i]= S.nextInt();
int sum=0;
float avg;
for(int i=0; i<length;i++)
sum = sum + array[i];
avg = (float) sum/length;
System.out.println("The sum is : " + sum);
System.out.println("The average is : " + avg);
}
}
INPUT/OUTPUT :
Enter the length of the array
5
Enter the elements of the array
1
2
3
4
5
The sum is : 15
The average is : 3.0
SOLUTION 02
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the order of Array 1");
int r1 = S.nextInt();
int c1 = S.nextInt();
System.out.println("Enter the order of Array 2");
int r2 = S.nextInt();
int c2 = S.nextInt();
if (r1 == r2 && c1 == c2) {
int[][] array1 = new int[r1][c1];
int[][] array2 = new int[r1][c1];
int[][] sum = new int[r1][c1];
System.out.println("Enter the elements of Array 1");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
array1[i][j] = S.nextInt();
}
}
System.out.println("Enter the elements of Array 2");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
array2[i][j] = S.nextInt();
}
}
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
sum[i][j] = array1[i][j] + array2[i][j];
}
}
System.out.println("The Sum of the Arrays is : ");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
} else
System.out.println("Arrays cant be added");
}
}
INPUT/OUTPUT :
Enter the order of Array 1
2
2
Enter the order of Array 2
2
2
Enter the elements of Array 1
1
2
2
1
Enter the elements of Array 2
1
2
2
1
The Sum of the Arrays is :
24
42
SOLUTION 03
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
int[] array = {1, 4, 8, 12, 31, 100, 42, 68, 35, 108};
System.out.println("Enter the Element to be searched");
int key = S.nextInt();
boolean flag = false;
int index=0;
for (int i = 0; i < 10; i++) {
if (array[i] == key) {
flag = true;
index = i;
break;
}
}
if (flag)
System.out.println("Element found at index : " + index);
else
System.out.println("Element not found");
}
}
INPUT/OUTPUT :
Enter the Element to be searched
12
Element found at index : 3
SOLUTION 04
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the Array");
int length = S.nextInt();
int[] array = new int[length];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < length; i++)
array[i] = S.nextInt();
int sum = 0;
for (int i = 0; i < length; i++)
if (array[i] % 2 == 0)
sum += array[i];
System.out.println("The sum of even numbers of Array is : " + sum);
}
}
INPUT/OUTPUT :
Enter the length of the Array
5
Enter the elements of the Array
1
4
2
5
3
The sum of even numbers of Array is : 6
SOLUTION 05
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
int[][] array = new int[3][3];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
array[i][j] = S.nextInt();
}
}
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j)
sum += array[i][j];
}
}
System.out.println("The sum of diagonal elements of Array is : " + sum);
}
}
INPUT/OUTPUT :
Enter the elements of the Array
1
2
3
4
5
6
7
8
9
The sum of diagonal elements of Array is : 15
SOLUTION 06
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the Array");
int length = S.nextInt();
int[] array = new int[length];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < length; i++)
array[i] = S.nextInt();
int temp;
for (int i = 0; i < length / 2; i++)
{
temp = array[i];
array[i] = array[length-i-1];
array[length-i-1] = temp;
}
System.out.println("The reversed array is : ");
for(int i=0 ; i<length; i++)
System.out.print(array[i] + " ");
}
}
INPUT/OUTPUT :
Enter the length of the Array
5
Enter the elements of the Array
1
3
5
7
9
The reversed array is :
97531
SOLUTION 07
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the Array");
int length = S.nextInt();
int[] array = new int[length];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < length; i++)
array[i] = S.nextInt();
int min = array[0];
for (int i = 0; i < length; i++)
if (array[i] < min)
min = array[i];
System.out.println("The smallest element of the array is : "+ min);
}
}
INPUT/OUTPUT :
Enter the length of the Array
5
Enter the elements of the Array
2
5
8
3
9
The smallest element of the array is : 2
SOLUTION 08
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
int[][] array = new int[3][3];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
array[i][j] = S.nextInt();
}
}
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (array[i][j] % 2 != 0)
sum += array[i][j];
}
}
System.out.println("The sum of odd elements of the array is : " + sum);
}
}
INPUT/OUTPUT :
Enter the elements of the Array
1
2
3
4
5
6
7
8
9
The sum of odd elements of the array is : 25
SOLUTION 09
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
int[][] array1 = new int[3][3];
int[][] array2 = new int[3][3];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
array1[i][j] = S.nextInt();
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
array2[i][j] = array1[j][i];
}
}
System.out.println("The transpose of the matrix is : ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
}
}
INPUT/OUTPUT :
Enter the elements of the Array
1
2
3
4
5
6
7
8
9
The transpose of the matrix is :
147
258
369
SOLUTION 10
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
int[][] array = new int[3][3];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
array[i][j] = S.nextInt();
}
}
int zero = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if(array[i][j] ==0)
zero++;
}
}
if(zero>=4)
System.out.println("The matrix is a Sparse matrix");
else
System.out.println("The matrix is not a Sparse matrix");
}
}
INPUT/OUTPUT :
Enter the elements of the Array
0
0
0
1
2
4
0
5
2
The matrix is a Sparse matrix
SOLUTION 11
import java.util.Scanner;
class Test {
public static boolean isPrime(int n) {
boolean flag = true;
if (n == 1)
return false;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
flag = false;
break;
}
}
return flag;
}
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the Array");
int length = S.nextInt();
int[] array = new int[length];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < length; i++) {
array[i] = S.nextInt();
}
int count = 0;
for (int i = 0; i < length; i++) {
if(isPrime(array[i]))
count++;
}
System.out.println("The number of prime numbers in the Array is : " + count);
}
INPUT/OUTPUT :
Enter the length of the Array
5
Enter the elements of the Array
2
3
4
7
8
The number of prime numbers in the Array is : 3
SOLUTION 12
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the Array");
int length = S.nextInt();
int[] array = new int[length];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < length; i++)
array[i] = S.nextInt();
int temp;
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("The second highest element of the Array is : " + array[length -
2]);
}
}
INPUT/OUTPUT :
Enter the length of the Array
8
Enter the elements of the Array
2
5
91
35
41
23
8
44
The second highest element of the Array is : 44
SOLUTION 13
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the Array");
int length = S.nextInt();
int[] array = new int[length];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < length; i++)
array[i] = S.nextInt();
int count=0;
for(int i=0;i<length;i++)
if(array[i] != 0)
count++;
System.out.println("The number of non-zero elements in the Array is : " + count);
}
}
INPUT/OUTPUT :
Enter the length of the Array
8
Enter the elements of the Array
0
0
0
2
3
1
6
0
The number of non-zero elements in the Array is : 4
SOLUTION 14
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the Array 1");
int length1 = S.nextInt();
float[] array1 = new float[length1];
System.out.println("Enter the elements of the Array 1");
for (int i = 0; i < length1; i++)
array1[i] = S.nextFloat();
System.out.println("Enter the length of the Array 2");
int length2 = S.nextInt();
float[] array2 = new float[length2];
System.out.println("Enter the elements of the Array 2");
for (int i = 0; i < length2; i++)
array2[i] = S.nextFloat();
float[] array3 = new float[length1 + length2];
for (int i = 0; i < length1 + length2; i++) {
if (i < length1)
array3[i] = array1[i];
else
array3[i] = array2[i-length1];
}
System.out.println("The merged array is : ");
for (int i = 0; i < length1 + length2; i++)
System.out.println(array3[i] + " ");
}
}
INPUT/OUTPUT :
Enter the length of the Array 1
3
Enter the elements of the Array 1
1.4
1.9
2.6
Enter the length of the Array 2
4
Enter the elements of the Array 2
5.4
6.9
8.33
9.11
The merged array is :
1.4
1.9
2.6
5.4
6.9
8.33
9.11
SOLUTION 15
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the both the Arrays");
int length = S.nextInt();
int[] array1 = new int[length];
System.out.println("Enter the elements of the Array 1");
for (int i = 0; i < length; i++)
array1[i] = S.nextInt();
int[] array2 = new int[length];
System.out.println("Enter the elements of the Array 2");
for (int i = 0; i < length; i++)
array2[i] = S.nextInt();
int [] array3 = new int[length];
for(int i=0; i<length; i++)
array3[i] = array1[i] + array2[i];
System.out.println("The added array is : ");
for(int i=0; i<length; i++)
System.out.print(array3[i] + " ");
}
}
INPUT/OUTPUT :
Enter the length of the both the Arrays
5
Enter the elements of the Array 1
23145
Enter the elements of the Array 2
47125
The added array is :
6 10 2 6 10
SOLUTION 16
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the rows and columns of Matrix 1");
int r1 = S.nextInt();
int c1 = S.nextInt();
System.out.println("Enter the rows and columns of Matrix 2");
int r2 = S.nextInt();
int c2 = S.nextInt();
if (c1 == r2) {
int[][] m1 = new int[r1][c1];
System.out.println("Enter the elements of Matrix 1");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
m1[i][j] = S.nextInt();
}
}
int[][] m2 = new int[r2][c2];
System.out.println("Enter the elements of the Matrix 2");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
m2[i][j] = S.nextInt();
}
}
int[][] m3 = new int[r1][c2];
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
m3[i][j] = 0;
for (int k = 0; k < c1; k++) {
m3[i][j] += m1[i][k] * m2[k][j];
}
}
}
System.out.println("The multiplied Matrix is : ");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
System.out.print(m3[i][j] + " ");
}
System.out.println();
}
} else
System.out.println("The matrices can’t be multiplied");
}
}
INPUT/OUTPUT :
Enter the rows and columns of Matrix 1
33
Enter the rows and columns of Matrix 2
33
Enter the elements of Matrix 1
123
456
789
Enter the elements of the Matrix 2
987
654
321
The multiplied Matrix is :
30 24 18
84 69 54
138 114 90
SOLUTION 17
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the rows and columns of Matrix 1");
int r1 = S.nextInt();
int c1 = S.nextInt();
System.out.println("Enter the rows and columns of Matrix 2");
int r2 = S.nextInt();
int c2 = S.nextInt();
if (r1 == r2 && c1==c2) {
int[][] m1 = new int[r1][c1];
System.out.println("Enter the elements of Matrix 1");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
m1[i][j] = S.nextInt();
}
}
int[][] m2 = new int[r2][c2];
System.out.println("Enter the elements of the Matrix 2");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
m2[i][j] = S.nextInt();
}
}
int[][] m3 = new int[r1][c1];
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
m3[i][j] += m1[i][j] - m2[i][j];
}
}
System.out.println("The subtracted Matrix is : ");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
System.out.print(m3[i][j] + " ");
}
System.out.println();
}
} else
System.out.println("The matrices cant be subtracted");
}
}
INPUT/OUTPUT :
Enter the rows and columns of Matrix 1
33
Enter the rows and columns of Matrix 2
33
Enter the elements of Matrix 1
12 1
36 8
4 5 -1
Enter the elements of the Matrix 2
32 0
09 5
6 4 12
The subtracted Matrix is :
-2 0 1
3 -3 3
-2 1 -13
SOLUTION 18
import java.util.Arrays;
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the Array");
int length = S.nextInt();
int[] array = new int[length];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < length; i++) {
array[i] = S.nextInt();
}
Arrays.sort(array);
System.out.println("The duplicate elements of the Array and their frequency is :");
int i, j;
for (i = 0; i < length; i++) {
int frequency = 1;
for (j = i + 1; j < length; j++) {
if (array[i] == array[j])
frequency++;
else
break;
}
if (frequency > 1)
System.out.println(array[i] + " Frequency -> " + frequency);
i = j-1;
}
}
}
INPUT/OUTPUT :
Enter the length of the Array
8
Enter the elements of the Array
23132411
The duplicate elements of the Array and their frequency is :
1 Frequency -> 3
2 Frequency -> 2
3 Frequency -> 2
SOLUTION 19
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Enter the length of the Array");
int length = S.nextInt();
int[] array = new int[length];
System.out.println("Enter the elements of the Array");
for (int i = 0; i < length; i++) {
array[i] = S.nextInt();
}
System.out.print("Alternate elements of the Array are : ");
for(int i =0;i<length; i=i+2)
System.out.print(array[i] + " ");
}
}
INPUT/OUTPUT :
Enter the length of the Array
8
Enter the elements of the Array
12345678
Alternate elements of the Array are : 1 3 5 7
SOLUTION 20
class Test {
public static void main(String args[]) {
int sum = 0;
for(int i=0;i<args.length;i++)
{
sum +=Integer.parseInt(args[i]);
}
int average = sum / args.length;
System.out.println("Average of " + args.length + " command line arguments is " +
average);
}
}
INPUT/OUTPUT :
Average of 3 command line arguments is 43