Asssignment 3:: 1. Implement Transpose of A Given Matrix

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

ASSSIGNMENT 3:

1. Implement transpose of a given matrix


PROGRAM:
import java.util.*;
class Transpose
{
static void Trans(int a[][],int n,int m)
{
int i,j;
int b[][]=new int[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[j][i]=a[i][j];
}
}
System.out.println("Transpose of matrix is:");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println(" ");
}
}
public static void main(String[] aa)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int a[][]=new int[3][3];
System.out.println("Enter a matrix of size "+n+" * "+m);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}
}
Trans(a,n,m);
}
}
OUTPUT:
2
2

Enter a matrix of size 2 * 2


12
34
Transpose of matrix is:
13
24

2. Implement multiplication of two Matrix


PROGRAM:
import java.util.*;
class Multi
{
public static void main(String[] aa)
{
int i,j,k,m,n,o,p;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of matrx a:");
m=sc.nextInt();
n=sc.nextInt();
System.out.println("Enter the size of matrix b:");
o=sc.nextInt();
p=sc.nextInt();
if(n==o)
{
int a[][]=new int[m][n];
int b[][]=new int[o][p];
int res[][]=new int[m][p];
System.out.println("Enter the matrxi a elements:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Enter the matrxi b elements:");
for(i=0;i<o;i++)
{
for(j=0;j<p;j++)
{
b[i][j]=sc.nextInt();
}
}
System.out.println("The result matrix is");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
int sum=0;
for(k=0;k<o;k++)
{
sum+=a[i][k]*b[k][j];
}
res[i][j]=sum;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
System.out.print(res[i][j]+" ");
}
System.out.println();
}
}
else {
System.out.println("Multiplication does not exist");
}
}
}
OUTPUT:
Enter the size of matrx a:
22
Enter the size of matrix b:
22
Enter the matrix a elements:
12
34
Enter the matrIx b elements:
43
21
The result matrix is
85
20 13

3. Implement a program to generate random password based on

customer name, age and id for banking applications

PROGRAM:
import java.util.*;

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("Your random password is:");
System.out.println(generatePassword(name,age,id));
sc.close();
}

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;
}
}
OUTPUT:
Enter name of customer:
Chandu
Enter age of customer:
22
Enter id of customer:
143
Your random password is:
a23d4242

You might also like