0% found this document useful (0 votes)
10 views2 pages

Dda

The document contains two Java programs: one for adding two matrices and another for generating the transpose of a matrix. Each program prompts the user for the dimensions and elements of the matrices, performs the required operation, and displays the result. Input validation is included to ensure the dimensions are positive.

Uploaded by

ISHIKA RAJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Dda

The document contains two Java programs: one for adding two matrices and another for generating the transpose of a matrix. Each program prompts the user for the dimensions and elements of the matrices, performs the required operation, and displays the result. Input validation is included to ensure the dimensions are positive.

Uploaded by

ISHIKA RAJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Q> Add two matrices.

import java.util.* ;

class MatAdd
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in) ;

int R, C, i, j ;

System.out.println("Enter the Order of the Matrices [R, C] : ") ;


R = sc.nextInt() ;
C = sc.nextInt() ;

if(R<=0 || C<=0)
System.out.println("Invalid Input.") ;
else
{
int A[][] = new int[R][C] ;
int B[][] = new int[R][C] ;

System.out.println("Enter the 1st Matrix : -") ;


for(i=0 ; i<R ; i++)
{
for(j=0 ; j<C ; j++)
{
A[i][j] = sc.nextInt() ;

}
}

System.out.println("Enter the 2 nd Matrix : -") ;


for(i=0 ; i<R ; i++)
{
for(j=0 ; j<C ; j++)
{
B[i][j] = sc.nextInt() ;

}
}

System.out.println("The Result is : -") ;


for(i=0 ; i<R ; i++)
{
for(j=0 ; j<C ; j++)
{
System.out.print( (A[i][j]+B[i][j]) + " \t" ) ;
}
System.out.println() ;
}

}}

1
Q> Generate the Transpose of a matri x.

import java.util.* ;

class Transpose
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in) ;

int R, C, i, j ;

System.out.println("Enter the Order of the Matrix [R C] : ") ;


R = sc.nextInt() ;
C = sc.nextInt() ;

if(R<=0 || C<=0)
System.out.println("Invalid Input.") ;
else
{
int A[][] = new int[R][C] ;

System.out.println("Enter the Matrix : -") ;


for(i=0 ; i<R ; i++)
{
for(j=0 ; j<C ; j++)
{
A[i][j] = sc.nextInt() ;

}
}

System.out.println("The Transpose is : -") ;


for(j=0 ; j<C ; j++)
{
for(i=0 ; i<R ; i++)
{
System.out.print( A[i][j] + " \t" ) ;
}
System.out.println() ;
}

}}

You might also like