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

1.

Implement transpose of a given matrix

import java.util.Scanner;

public class Transpose {

static void transpose(int a[][], int b[][], int n1, int n2) {
int i, j;
for (i = 0; i < n1; i++)
for (j = 0; j < n2; j++)
b[j][i] = a[i][j];
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of columns:");
int n1 = sc.nextInt();
System.out.println("Enter the number of rows:");
int n2 = sc.nextInt();
int[][] a = new int[n1][n2];
System.out.println("Enter the elements of matrix:");
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
a[i][j] = sc.nextInt();

}
}

int[][] b = new int[n2][n1];


transpose(a, b, n1, n2);

System.out.print("Transpose of given matrix is \n");


for (int i = 0; i < n2; i++) {
for (int j = 0; j < n1; j++)
System.out.print(b[i][j] + " ");
System.out.print("\n");
}
sc.close();
}
}

Output:
Enter the number of columns:
4
Enter the number of rows:
5
Enter the elements of matrix:
1551
2688
8862
1551
5678
Transpose of given matrix is
1661
5825
5816
1857
2858
2. Implement multiplication of two Matrix

import java.util.Scanner;
public class Multiplicationmatrix {
public static void main(String args[]) {
int r1, r2, c1, c2, i, j, k, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows in Matrix 1:");
r1 = sc.nextInt();
System.out.println("Enter the number columns in Matrix 1:");
c1 = sc.nextInt();
System.out.println("Enter the number of rows in Matrix 2:");
r2 = sc.nextInt();
System.out.println("Enter the number of columns in Matrix 2:");
c2 = sc.nextInt();

if (c1 == r2) {
int n1[][] = new int[r1][c1];
int n2[][] = new int[r2][c2];
int res[][] = new int[r1][c2];

System.out.println("Enter the elements of Matrix 1");

for (i = 0; i < r1; i++) {

for (j = 0; j < c1; j++)


n1[i][j] = sc.nextInt();

}
System.out.println("Enter the elements of Matrix 2");

for (i = 0; i < r2; i++) {

for (j = 0; j < c2; j++)


n2[i][j] = sc.nextInt();
}

System.out.println("Output matrix");
for (i = 0; i < r1; i++)

for (j = 0; j < c2; j++) {


sum = 0;
for (k = 0; k < r2; k++) {
sum += n1[i][k] * n2[k][j];

}
res[i][j] = sum;
}
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++)
System.out.print(res[i][j] + " ");

System.out.println();
}
} else
System.out.print("Multipication does not exist ");
sc.close();

Output:

Enter the number of rows in Matrix 1:


4
Enter the number columns in Matrix 1:
4
Enter the number of rows in Matrix 2:
4
Enter the number of columns in Matrix 2:
4
Enter the elements of Matrix 1
8800
2626
1551
0088
Enter the elements of Matrix 2
2626
0088
1551
8088
Output matrix
16 48 80 112
54 22 110 110
15 31 75 59
72 40 104 72
3. Implement a program to generate random password based on

customer name, age and id for banking applications.

import java.util.Random;
import java.util.Scanner;

public class Randompassword {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.println("Enter name of the customer:");
String name=sc.nextLine();
System.out.println("Enter the age of customer:");
String age=sc.nextLine();
System.out.println("Enter the id of the customer:");
String id=sc.nextLine();
System.out.println(generatePassword(name,age,id));
sc.close();
}

private static char[] generatePassword(String name,String age, String id) {

name = name.replaceAll(" ", "");


String combinedChars = name + age + id ;
Random random = new Random();
char[] password = new char[8];
password[0] = name.charAt(random.nextInt(name.length()));
password[1] = age.charAt(random.nextInt(age.length()));
password[2] = id.charAt(random.nextInt(id.length()));
for (int i = 3; i < 8; i++) {
password[i] =
combinedChars.charAt(random.nextInt(combinedChars.length()));
}
return password;
}
}

Output:
Enter name of the customer:
shree
Enter the age of customer:
20
Enter the id of the customer:
9513902
s00hrs90

You might also like