Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ASSIGNMENT 3

1. Implement transpose of a given matrix?

import java.util.Scanner;

public class Transpose {

static void transpose(int A[][], int B[][], int N, int M) {


int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < M; j++)
B[j][i] = A[i][j];
}

public static void main(String[] args) {


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

}
}

int[][] B = new int[M][N];

transpose(A, B, N, M);

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


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

2. Implement multiplication of two Matrix?

import java.util.Scanner;

public class Multiplication {

public static void main(String args[]) {


int row1, row2, column1, column2, i, j, k, sum;
Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows in matrix 1:");


row1 = sc.nextInt();

System.out.println("Enter the number columns in matrix 1:");


column1 = sc.nextInt();
System.out.println("Enter the number of rows in matrix 2:");
row2 = sc.nextInt();

System.out.println("Enter the number of columns in matrix


2:");
column2 = sc.nextInt();

if (column1 == row2) {

int m1[][] = new int[row1][column1];


int m2[][] = new int[row2][column2];
int res[][] = new int[row1][column2];

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

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

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


m1[i][j] = sc.nextInt();
}
System.out.println("Enter the elements of matrix2");

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

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


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

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

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


sum = 0;
for (k = 0; k < row2; k++) {
sum += m1[i][k] * m2[k][j];

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

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

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 customer:");
String name=sc.nextLine();
System.out.println("Enter age of customer:");
String age=sc.nextLine();
System.out.println("Enter id of 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[1] = age.charAt(random.nextInt(age.length()));
password[0] = name.charAt(random.nextInt(name.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;
}
}

You might also like