Assignment of Array LabsheetJava
Assignment of Array LabsheetJava
Assignment:4
import java.util.Scanner;
int num=sc.nextInt();
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
for(int i=0;i<num;i++){
System.out.println(arr[i]);
Output:
Enter 5 numbers: 33 5 77 2 10
33
77
10
import java.util.Scanner;
int num=sc.nextInt();
double sum=0,average=0;
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
sum+=arr[i];
average=sum/num;
Output:
Enter 5 numbers: 2 3 44 1 6
import java.util.Scanner;
public class Array3{
public static void main(String args[]){
int i,num, item, array[];
Scanner input = new Scanner(System.in);
System.out.println("Enter size of the array:");
num = input.nextInt();
array = new int[num];
System.out.println("Enter " + num + " integers");
for ( i = 0; i < num; i++)
array[i] = input.nextInt();
System.out.println("Enter the search value:");
item = input.nextInt();
for ( i = 0; i < num; i++)
{
if (array[i] == item)
{
System.out.println(item+" is present at location "+(i+1));
break;
}
}
if ( i == num)
System.out.println(item + " doesn't exist in array.");
}
}
Output:
Enter size of the array:
5
Enter 5 integers
33 2 1 6 3
Enter the search value:
6
6 is present at location 4
import java.util.Scanner;
int num=sc.nextInt();
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
int x=sc.nextInt();
Output:
import java.util.Scanner;
public class CopyingArray{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of the array:");
int num=sc.nextInt();
int[] firsta=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
firsta[i]=sc.nextInt();
}
int [] seconda=new int[num];
for(int i=0;i<firsta.length;i++){
seconda[i]=firsta[i];
System.out.println("The copy part of first array of index "+i+" is
"+seconda[i]);
}
}
}
Output:
Enter size of the array:
4
Enter 4 numbers: 2 4 7 8
The copy part of first array of index 0 is 2
The copy part of first array of index 1 is 4
The copy part of first array of index 2 is 7
The copy part of first array of index 3 is 8
int max=arr[0];
int min=arr[0];
for(int i=1;i<arr.length;i++){
if(arr[i]>max){
max=arr[i];
else if(arr[i]<min){
min=arr[i];
}
Output:
int max=arr[0];
int min=arr[0];
for(int i=1;i<arr.length;i++){
if(arr[i]>max){
max=arr[i];
else if(arr[i]<min){
min=arr[i];
}
Output:
The difference between maximum and minimum value is 63
import java.util.Scanner;
int num=sc.nextInt();
System.out.printf("Enter %d numbers:",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
int size=arr.length;
for(int n:arr){
System.out.print(n+" ");
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
for(int n:arr){
System.out.print(n+" ");
}
Output:
Enter 5 numbers:1 55 3 88 4
1 55 3 88 4
Array after Sorting 1
1 3 55 4 88
1 3 4 55 88
1 3 4 55 88
1 3 4 55 88
1 3 4 55 88
int arr[]={2,66,4,8,555};
int largest=arr[0];
int secondLargest=arr[0];
for(int i=0;i<arr.length;i++){
if(arr[i]>largest){
secondLargest=largest;
largest=arr[i];
else if(arr[i]>secondLargest){
secondLargest=arr[i];
}
Output:
The second highest value is: 66
import java.util.Scanner;
import java.util.Arrays;
int num=sc.nextInt();
int [] arr=new int[num];
System.out.printf("Enter %d numbers:",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
int size=arr.length;
for(int i=0;i<size/2;i++){
int a=arr[i];
arr[i]=arr[arr.length-i-1];
arr[arr.length-i-1]=a;
System.out.println("Reverse value:"+Arrays.toString(arr));
Output:
Enter 7 numbers:1 2 3 4 5 6 7
Reverse value:[7, 6, 5, 4, 3, 2, 1]
11. Write a Java program to find the duplicate values of an array
of integer values.
import java.util.Scanner;
public class DuplicateArray{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of array:");
int num=sc.nextInt();
int [] arr=new int[num];
System.out.printf("Enter %d numbers:",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]==arr[j]){
System.out.println("Duplicate elements is: "+arr[i]);
}
}
}
}
}
Output:
Enter size of array:
10
Enter 10 numbers:2 5 7 3 6 2 4 3 1 3
Duplicate elements is: 2
Duplicate elements is: 3
Duplicate elements is: 3
Duplicate elements is: 3
12. Write a Java program to find the common elements between
two arrays of integers.
public class CommonArray{
public static void main(String [] args){
int []a={1,2,3,4,5};
int []b={1,3,5,7,9};
for (int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
if(a[i]==b[j]){
System.out.println(a[i]+" ");
break;
}
}
}
}
}
Output:
1
3
5
13. Write a Java program to add two matrices of the same size.
import java.util.Scanner;
int row=sc.nextInt();
int column=sc.nextInt();
System.out.printf("Enter element:");
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
a[i][j]=sc.nextInt();
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
b[i][j]=sc.nextInt();
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
c[i][j]=a[i][j]+b[i][j];
}
Output:
33
array [0][1]:2
array [0][2]:3
array [1][0]:4
array [1][1]:5
array [1][2]:6
array [2][0]:7
array [2][1]:8
array [2][2]:9
array [0][0]:9
array [0][1]:8
array [0][2]:7
array [1][0]:6
array [1][1]:5
array [1][2]:4
array [2][0]:3
array [2][1]:2
array [2][2]:1
14. Write a java program to take a 2d array input from user and
print the sum of each row, column and total sum in as shown
below:
import java.util.Scanner;
public class SumOfRowColumn{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of matrix:");
int m=sc.nextInt();
int n=sc.nextInt();
int [] [] arr=new int[m][n];
System.out.printf("Enter the number");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
System.out.printf("array [%d][%d]:",i, j);
arr[i][j]=sc.nextInt();
}
}
int sumRow=0;
for(int i=0;i<m;i++){
sumRow=0;
for(int j=0;j<n;j++){
sumRow+=arr[i][j];
System.out.print(arr[i][j]+" ");
}
System.out.println(sumRow+" ");
}
int sumCol=0;
int sum=0;
for(int j=0;j<n;j++){
sumCol=0;
for(int i=0;i<m;i++){
sumCol+=arr[i][j];
}
sum+=sumCol;
System.out.print(sumCol+" ");
}
System.out.println(sum);
}
}
Output:
Enter size of matrix:
33
Enter the numberarray [0][0]:1
array [0][1]:2
array [0][2]:3
array [1][0]:4
array [1][1]:5
array [1][2]:6
array [2][0]:7
array [2][1]:8
array [2][2]:9
1 2 3 6
4 5 6 15
7 8 9 24
12 15 18 45
15. Write a Java program to input two matrices order, check if the
order is eligible to multiply, if yes input the two matrices,
multiply them and display the result.
import java.util.Scanner;
if (n != p)
System.out.println("The matrices can't be multiplied with each
other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
multiply[i][j] = sum;
sum = 0;
}
}
System.out.print("\n");
}
}
}
}
Output:
Enter the number of rows and columns of first matrix
3
3
Enter elements of first matrix
1
2
1
3
4
6
8
9
5
Enter the number of rows and columns of second matrix
3
3
Enter elements of second matrix
2
5
8
2
9
0
3
2
5
Product of the matrices:
9 25 13
32 63 54
49 131 89
import java.util.Scanner;
int m=sc.nextInt();
int n=sc.nextInt();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
arr[i][j]=sc.nextInt();
for(int j=0;j<n;j++){
transpose[j][i]=arr[i][j];
for(int j=0;j<m;j++){
System.out.print(transpose[i][j]+" ");
System.out.println();
}
Output:
33
array [0][1]:2
array [0][2]:1
array [1][0]:6
array [1][1]:8
array [1][2]:4
array [2][0]:6
array [2][1]:2
array [2][2]:9
166
282
149
17. Write a Java program to find all pairs of elements in an array
whose sum is equal to a specified number.
import java.util.Scanner;
public class PairOfSum{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of elements");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int a=0;a<num;a++){
arr[a]=sc.nextInt();
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]+arr[j]==arr[a]){
System.out.println(arr[i]+"+"+arr[j]+ " ="+arr[a]);
}
}
}
}
}
}
Output:
Enter size of array:
5
Enter 5 numbers: 1 2 3 4 5
1+0 =1
1+0 =1
1+0 =1
1+0 =1
2+0 =2
2+0 =2
2+0 =2
1+2 =3
3+0 =3
3+0 =3
1+3 =4
4+0 =4
1+4 =5
2+3 =5
int count=0;
if(arr[i]%2==0){
System.out.println(arr[i]+":Even Number");
}
else {
System.out.println(arr[i]+":Odd Number");
}
}
}
}
Output:
Enter size of the array:
5
Enter 5 numbers: 1
1:Odd Number
23
23:Odd Number
4
4:Even Number
56
56:Even Number
7
7:Odd Number
import java.util.Scanner;
int num=sc.nextInt();
boolean checkOne=false,checkTwo=false;
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
for(int i=0;i<num;i++){
if(arr[i]==65){
checkOne=true;
}
else if(arr[i]==77){
checkTwo=true;
if(checkOne==true &&checkTwo==true){
else{
}
Output:
Enter size of the arrays:
6
Enter 6 numbers: 22
44
66
77
87
65
array contain 65 & 77.
24. Write a Java program to find the sum of the two elements of a
given array which is equal to a given integer.
Sample array: [1,2,4,5,6]
Target value: 6.
import java.util.Scanner;
public class TargetedSum{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of array:");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
System.out.println("Enter the target sum value:");
int x=sc.nextInt();
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]+arr[j]==x){
System.out.println(arr[i]+"+"+arr[j]+ " ="+x);
}
}
}
}
}
Output:
Enter size of array:
5
Enter 5 numbers: 12
44
7
2
9
Enter the target sum value:
9
7+2 =9
25. Write a Java program to print all the LEADERS in the array.
Note: An element is leader if it is greater than all the elements to
its right side.
import java.util.Scanner;
public class Leaders{
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of the array:");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int a=0;a<num;a++){
arr[a]=sc.nextInt();
}
int size=arr.length;
for (int i = 0; i < size; i++)
{
int j;
for (j = i + 1; j < size; j++)
{
if (arr[i] <= arr[j])
break;
}
if (j == size)
System.out.print(arr[i] + " " +"is the leaders.");
}
}
}
Output:
Enter size of the array:
5
Enter 5 numbers: 1 55 22 9 6
55 is the leaders.22 is the leaders.9 is the leaders.6 is the leaders.
26. Write a Java program to find smallest and second smallest
elements of a given array.
27. Write a Java program to segregate all 0s on left side and all 1s
on right side of a given array of 0s and 1s.
Output:
0001111
}
System.out.print("\nThe rotated Array is: ");
arr[0] = last;
for (i = 0; i < n; ++i){
System.out.print(arr[i]+" ");
}
}
}
Output:
The original array is: 1 2 3 4 5 6 7 8
The rotated Array is: 8 1 2 3 4 5 6 7
import java.util.Scanner;
import java.util.Arrays;
public class SampleMaxi{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter size of the array:");
int num=sc.nextInt();
int[] firstArr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
firstArr[i]=sc.nextInt();
}
int size=firstArr.length;
Arrays.sort(firstArr);
System.out.println("sorted Array ::"+Arrays.toString(firstArr));
int [] secondArr=new int[num];
for(int i=0;i<size/2;i++){
secondArr[i]=firstArr[i];
System.out.println(" \nFirst copying part:"+secondArr[i]);
}
int [] thirdArr=new int[num];
for(int i=num-1;i>=size/2;i--){
thirdArr[i]=firstArr[i];
System.out.println("Second copying part:"+thirdArr[i]);
}
int [] result=new int[num];
int j=num-1,k=0;
for( int i=0;i<result.length;i++){
if(i%2==0){
result[i]=thirdArr[j--];
}
else{
result[i]=secondArr[k++];
}
}
for(int i=0;i<num;i++){
System.out.println(result[i]);
}
}
}
Output:
Enter size of the array:
5
Enter 5 numbers: 33 2 66 11 90
sorted Array ::[2, 11, 33, 66, 90]
import java.util.Arrays;
import java.util.Scanner;
public class Sorting0And1{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("Size of array:");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d number only 0 and 1 :",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
int k = 0;
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == 0) {
arr[k++] = 0;
}
}
for (int i = k; i < arr.length; i++) {
arr[k++] = 1;
}
System.out.println("arrays after sorting is"+Arrays.toString(arr));
}
}
Output:
Size of array:
5
Enter 5 number only 0 and 1 :0 1 0 1 0
arrays after sorting is [0, 0, 0, 1, 1]
32. Given two sorted arrays A and B of size p and q, write a Java
program to merge elements of A with B by maintaining the
sorted order i.e. fill A with first p smallest elements and fill B
with remaining elements.
import java.util.Scanner;
import java.util.Random;
int num=sc.nextInt();
for(int i=0;i<num;i++){
firstArr[i]=sc.nextInt();
int size=firstArr.length;
Random rand=new Random();
int random=rand.nextInt(size);
for(int i=0;i<num;i++){
int temp=firstArr[i];
firstArr[i]=firstArr[random];
firstArr[random]=temp;
for(int i=0;i<size;i++){
System.out.println(firstArr[i]+" ");
}
Output:
Enter the size of First Array:
5
Enter 5 numbers: 33 4 2 6 1
4
1
33
2
6
import java.util.Scanner;
public class MultiplyAllElements{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the size of First Array:");
int num=sc.nextInt();
int[] arr=new int[num];
System.out.printf("Enter %d numbers: ",num);
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
int size=arr.length;
int product=1;
int [] result=new int[size];
for(int i=0;i<size;i++){
product*=arr[i];
}
for(int i=0;i<size;i++){
result[i]=product/arr[i];
}
System.out.printf("Array with product of every other element:”);
Output: