Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Practice Program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

MATRIX MULTIPLICATION TAKING DATA FROM USER:

import java.util.Scanner; b[i][j] = input.nextInt();


public class MatixMultiplication }
{ }
public static void main(String args[]) System.out.println("Multiplying the
{ matrices...");
int n; for (int i = 0; i < n; i++)
Scanner input = new Scanner(System.in); {
System.out.println("Enter the base of squared for (int j = 0; j < n; j++)
matrices"); {
n = input.nextInt(); for (int k = 0; k < n; k++)
int[][] a = new int[n][n]; {
int[][] b = new int[n][n]; c[i][j] = c[i][j] + a[i][k] * b[k][j];
int[][] c = new int[n][n]; }
System.out.println("Enter the elements of 1st }
martix row wise \n"); }
for (int i = 0; i < n; i++) System.out.println("The product is:");
{ for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
{ for (int j = 0; j < n; j++)
a[i][j] = input.nextInt(); {
} System.out.print(c[i][j] + " ");
} }
System.out.println("Enter the elements of 2nd System.out.println();
martix row wise \n"); }
for (int i = 0; i < n; i++) input.close();
{ }
for (int j = 0; j < n; j++) }
{
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows for the first matrix: ");
int rowsA = input.nextInt();
System.out.print("Enter the number of columns for the first matrix: ");
int colsA = input.nextInt();

System.out.print("Enter the number of rows for the second matrix: ");


int rowsB = input.nextInt();
System.out.print("Enter the number of columns for the second matrix: ");
int colsB = input.nextInt();

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;
}

int[][] firstMatrix = new int[rowsA][colsA];


int[][] secondMatrix = new int[rowsB][colsB];

System.out.println("Enter the elements of the first matrix:");


for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
firstMatrix[i][j] = input.nextInt();
}
}

System.out.println("Enter the elements of the second matrix:");


for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
secondMatrix[i][j] = input.nextInt();
}
}

input.close();

// Check if multiplication is possible and multiply matrices


if (colsA != rowsB) {
System.out.println("Matrix multiplication is not possible.");
} else {
int[][] resultMatrix = multiplyMatrices(firstMatrix, secondMatrix);
// Display the result
System.out.println("Result Matrix:");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
System.out.print(resultMatrix[i][j] + " ");
}
System.out.println();
}
}
}

public static int[][] multiplyMatrices(int[][] firstMatrix, int[][] secondMatrix) {


int rowsA = firstMatrix.length;
int colsA = firstMatrix[0].length;
int colsB = secondMatrix[0].length;

int[][] resultMatrix = new int[rowsA][colsB];

for (int i = 0; i < rowsA; i++) {


for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
resultMatrix[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}

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();

//Displaying elements of array arr2


System.out.println("Elements of new array: ");
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i] + " ");
}
}
}
Java Program to Sort an Array in Ascending
Order
import java.util.Scanner;
public class JavaExample
{
public static void main(String[] args)
{
int count, temp;

//User inputs the array size


Scanner scan = new Scanner(System.in);
System.out.print("Enter number of elements you want in the array: ");
count = scan.nextInt();

int num[] = new int[count];


System.out.println("Enter array elements:");
for (int i = 0; i < count; i++)
{
num[i] = scan.nextInt();
}
scan.close();
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++) {
if (num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
System.out.print("Array Elements in Ascending Order: ");
for (int i = 0; i < count - 1; i++)
{
System.out.print(num[i] + ", ");
}
System.out.print(num[count - 1]);
}
}
//Type Casting Program in Java
import java.util.Scanner;
public class TypeCasting
{
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: ");
int i=sc.nextInt();
// widening or automatic type conversion
long l = i;
float f = l;
double d= f;
System.out.println("After widening or automatic type conversion values are: ");
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
System.out.println("Double value "+d);

}
}

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;

System.out.println("After narrowing or explicit type conversion values are: ");


System.out.println("Double value: "+d);
//fractional part lost
System.out.println("Float value: "+f);
System.out.println("Long value: "+l);
//fractional part lost
System.out.println("Int value: "+i);
}
}

OUTPUT:

Enter the number: 123.541346731783


After narrowing or explicit type conversion values are:
Double value: 123.541346731783
Float value: 123.54134
Long value: 123
Int value: 123

You might also like