Java Practice Program
Java Practice Program
Java Practice Program
if (colsA != rowsB) {
System.out.println("Matrix multiplication is not possible. Number of columns in the first matrix must
be equal to the number of rows in the second matrix.");
input.close();
return;
}
input.close();
return resultMatrix;
}
}
Java Program to Find Transpose of a Matrix
import java.util.Scanner;
public class Transpose
{
public static void main(String args[])
{
int i, j;
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
int array[][] = new int[row][column];
System.out.println("Enter matrix:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
System.out.println("The above matrix before Transpose is ");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("The above matrix after Transpose is ");
for(i = 0; i < column; i++)
{
for(j = 0; j < row; j++)
{
System.out.print(array[j][i]+" ");
}
System.out.println(" ");
}
}
}
Java Program to Delete an Element from an Array
import java.util.Scanner;
public class Delete
{
public static void main(String[] args)
{
int n, x, flag = 1, loc = 0;
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();
}
System.out.print("Enter the element you want to delete:");
x = s.nextInt();
for (int i = 0; i < n; i++)
{
if(a[i] == x)
{
flag =1;
loc = i;
break;
}
else
{
flag = 0;
}
}
if(flag == 1)
{
for(int i = loc+1; i < n; i++)
{
a[i-1] = a[i];
}
System.out.print("After Deleting:");
for (int i = 0; i < n-2; i++)
{
System.out.print(a[i]+",");
}
System.out.print(a[n-2]);
}
else
{
System.out.println("Element not found");
}
}
}
Java Program to Merge Two Arrays
import java.io.BufferedReader; catch (IOException e)
import java.io.IOException; {
import java.io.InputStreamReader; System.out.println("Invalid
import java.util.Arrays; input");
return;
public class InOrderMerge { }
// Function to merge the arrays int[] arrayOne = new int[n];
static int[] mergeArrays(int[] int[] arrayTwo = new int[m];
arrayOne,int[] arrayTwo) System.out.println("Enter the
{ first array elements");
int totalLength=arrayOne.length int i,j;
+arrayTwo.length; for(i=0; i<arrayOne.length; i+
int[] c=new int[totalLength]; +){
int j,k,index=0; try {
j=0; arrayOne[i] =
k=0; Integer.parseInt(br.readLine());
while((j!=arrayOne.length) && }
(k!=arrayTwo.length)){ catch (IOException e)
{
if(arrayOne[j]<=arrayTwo[k])
{ System.out.println("Invalid array
c[index++]=arrayOne[j+ element. Enter it again");
+]; i--;
} }
else }
{ System.out.println("Enter the
c[index++]=arrayTwo[k+ second array elements");
+]; for(i=0; i<arrayTwo.length; i+
} +){
} try {
while(k!=arrayTwo.length) arrayTwo[i] =
{ Integer.parseInt(br.readLine());
c[index++]=arrayTwo[k++]; }
} catch (IOException e)
while(j!=arrayOne.length) {
{
c[index++]=arrayOne[j++]; System.out.println("Invalid array
} element. Enter it again");
return c; i--;
} }
// Function to read input and }
display the output Arrays.sort(arrayOne);
public static void main(String[] Arrays.sort(arrayTwo);
args){ int[]
BufferedReader br = new mergedArray=mergeArrays(arrayOne,arrayT
BufferedReader(new wo);
InputStreamReader(System.in)); System.out.println("The merged
int m, n; array is");
System.out.println("Enter the for(i=0;i<mergedArray.length;i+
size of the two arrays"); +)
try { {
n =
Integer.parseInt(br.readLine()); System.out.print(mergedArray[i]+" ");
m = }
Integer.parseInt(br.readLine()); }
} }
Java Program to Check if a Matrix is a Sparse Matrix
import java.util.Scanner;
public class Sparse
{
public static void main(String args[])
{
int i, j, zero = 0, count = 0;
int array[][] = new int[10][10];
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
System.out.println("Enter matrix:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
if(array[i][j] == 0)
{
zero++;
}
else
{
count++;
}
}
}
if(zero>count)
{
System.out.println("the matrix is sparse matrix");
}
else
{
System.out.println("the matrix is not a sparse matrix");
}
}
}
Program to copy all elements of one array into
another array
public class CopyArray {
public static void main(String[] args) {
//Initialize array
int [] arr1 = new int [] {1, 2, 3, 4, 5};
//Create another array arr2 with size of arr1
int arr2[] = new int[arr1.length];
//Copying all elements of one array into another
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
//Displaying elements of array arr1
System.out.println("Elements of original array: ");
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + " ");
}
System.out.println();
}
}
OUTPUT:
Enter the number: 50
After widening or automatic type conversion values are:
Int value 50
Long value 50
Float value 50.0
Double value 50.0
//Type Casting Program perform narrowing or explicit type conversion in java.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
// create an object of Scanner class
Scanner sc = new Scanner(System.in);
// ask users to enter the number
System.out.println("Enter the number: ");
double d=sc.nextDouble();
// narrowing or explicit type conversion
float f=(float)d;
//explicit type casting
long l = (long)d;
//explicit type casting
int i = (int)l;
OUTPUT: