Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Practical Solution 24-25 (2d Array)

2D array solution

Uploaded by

rizviabbas752
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 views7 pages

Practical Solution 24-25 (2d Array)

2D array solution

Uploaded by

rizviabbas752
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/ 7

import java.util.

*;
class Questionl
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of square matrix 3<M<10");
int M=sc.nextInt();
if(M>3 && M<10)
{
int A[][]=new int[M][M];
System.out.println("Enter "+(M*M)+" elements");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
System.out.print(A[i][j]+" ");
}
System.out.println();
}
int sum=0;
System.out.println("DIAGONAL ELEMENTS:");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
if(i==j || i+j==M-1)
{
sum=sum+A [i][j];
System.out.print(A[i][j]+" ");
}
else
System.out.print(" ");
}
System.out.println();
}
System.out.println("SUM OF DIAGONAL ELEMENTS:"+sum);
}
else
System. out.println("Invalid Size:");
}
}
************************************************************************************************************************

import java.util.*;
class Question4
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of square matrix 3<M<10");
int M=sc.nextInt();
if(M>2 && M<10)
{
int A[][]=new int[M][M];
System.out.println("Enter "+(M*M)+" elements");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("Original matrix:");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
System.out.print(A[i][j]+" ");
}
System.out.println();
}
int R[][] =new int[M][M]; int sum=0;
System.out.println("Matrix After Rotation:");
for(int i=0;i<M;i++)
{
int k=M-1;
for(int j=0;j<M;j++)
{
R[i][j]=A[k][i];
System.out.print(R[i][j]+ " ");
k--;
}
System.out.println();
}
sum=A[0] [0]+A[0][M- 1]+A[M-1 ] [0]+A[M-1] M-1 ];
System.out.println("Sum of corner Elements:"+sum);
}
else
System. out.println("Invalid Size:");
}
}
************************************************************************************************************************

import
java.util.*;
class Question7
{

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of square matrix 3<M<10");
int M=sc.nextInt(); if(M>2 && M<10)
{
int A[][]=new int[M][M];
System.out.println("Enter "+(M*M)+" elements");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("Original matrix:"); for(int
i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
System.out.print(A[i][j]+" ");
}
System.out.println();
}
int sym=1;
outer:for(int i=0;i<M/2;i++)
{
for(int j=0;j<M/2;j++)
{
if(A[i][j]!=A[j][i])
{
sym=0; break
outer;
}
}
}
if(sym==1)
System.out.println("The Given Matrix is Symmetric Matrix");
else
System.out.println("The Given Matrix is not a Symmetric Matrix");
int left=0,right=0; for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
if(i==j)
left=left+A[i][j ];
if(i+j==M-1)
right=right+A[i] [j ];
}
}
System.out.println("Sum of Elements of left diagonal are:"+right);
System.out.println("Sum of Elements of right diagonal are:"+left);
}
else
System.out.println("Invalid Size:");
}
}
************************************************************************************************************************

import java.util.*;
class Question9
{

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
System.out.println(nEnter size of square matrix 2<M<10");
int M=sc.nextInt(); if(M>2 && M<20)
{
int A[][]=new int[M][M],mirr[][]=new int[M][M];
System.out.println("Enter "+(M*M)+" elements");
for(int i=0;i<M;i++)
{
for(int j=0,k=M-1;j<M;j++,k--)
{
A[i][j]=sc.nextInt();
mirr[i][k]=A[i][j];
}
}
System.out.println("Original matrix:"); for(int
i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
System.out.print(A[i][j]+" ");
}
System.out.println();
}
System.out.println("Mirror IMAGE matrix:");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
System.out.print(mirr[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("Size out of range");
}
}
************************************************************************************************************************

import java.util.*;
class Question12
{

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of rows and columns 2<M<20 and 2<M<20");
int M=sc.nextInt();
int N=sc.nextInt();
if(M>2 && M<20 && N>2 && N<20)
{
int A[][]=new int[M][M];
int single[]=new int[M*N],k=0;
System.out.println("Enter "+(M*N)+" elements");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
A[i][j]=sc.nextInt();
single[k++]=A[i][j];
}
}
System.out.println("Original matrix:");
int max=A[0] [0],x1=0,y1=0,min=A[0][0],x2=0,y2=0;
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
if(max<A[i][j])
{
max=A[i][j];
x1=i;
y1=j;
}
if(min>A[i][j])
{
min=A[i][j];
x2=i;
y2=j;
}
System.out.print(A[i][j]+" ");
}
System.out.println();
}
for(int i=0;i<k-1;i++)
{
for(int j=0;j<k-1;j++)
{
if(single[j ]>single[j+1])
{
int t=single[j]; single[j
]=single[j+1];
single[j+1]=t;
}
}
}
k=0;
System.out.println("Rearrange matrix:");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
A[i][j]=single[k++];
System.out.print(A[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("Size out of range");
}
}

You might also like