Program 1
Define a class to accept values into a 3×3 array and check if it is a special array. An array is a special array if the sum of
the even elements = sum of the odd elements.
Example:A[][]={{ 4 ,5, 6}, { 5 ,3, 2}, { 4, 2, 5}};
Sum of even elements = 4+6+2+4+2 =18 Sum of odd elements= 5+5+3+5=18
import java.util.*;
public class special
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the array elements");
int a[][]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=s.nextInt();
}}
int even=0,odd=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(a[i][j]%2==0)
even=even+a[i][j];
else
odd=odd+a[i][j];
}
}
if(even==odd)
System.out.println("Array is a special array");
else
System.out.println("Array is not a special array");
}
}
Program 2
// Write a program to enter numbers in a double dimensional array of size 3*3 and display maximum
and minimum number present in the array.
import java.util.*;
public class min_max_dda
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[][]=new int[3][3];
System.out.println("enter the array element");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
int max=a[0][0],min=a[0][0];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(a[i][j]>max)
max=a[i][j];
if(a[i][j]<min)
min=a[i][j];
}}
System.out.println("the maximum number is"+max);
System.out.println("the minimum number is"+min);
}
}
Program 3
/* write a program to create a matrix of size 3*3 and find the transpose of the matrix.
* Transpose of a matrix is obtained by changing rows to columns and columns to rows.
Matrix
134
243
345
Printing Matrix After Transpose:
123
344
435
*/
import java.util.*;
public class transpose
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int a[][]=new int[3][3];
System.out.println("Enter array elements");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Matrix");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t ");
}
System.out.println();
}
System.out.println("Printing Matrix After Transpose:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[j][i]+" ");
}
System.out.println();
}
}
}
Program 4
/* Write a program in Java to enter natural numbers in a double dimensional array m x n
* (where m is the number of rows and n is the number of columns).
* Display the new matrix in such a way that the new matrix is
* the mirror image of the original matrix.
8 15 9 18
9 10 7 6
10 8 11 13
12 16 17 19
Sample Input
18 9 15 8
6 7 10 9
13 11 8 10
19 17 16 12
Sample Output
*/
import java.util.*;
public class MirrorImage
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows m ");
int m = sc.nextInt();
System.out.print("Enter number of columns n ");
int n = sc.nextInt();
int a[][] = new int[m][n];
System.out.println("Enter array elements");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println(" Matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
System.out.println("Mirror Image of Matrix :");
for (int i = 0; i < m; i++)
{
for (int j = n-1; j >= 0; j--)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}
Program 5
/* Write a program to declare a square matrix a[ ] [ ] of order (M x M)
(a) Display the original matrix.
(b) Check if the given matrix is Symmetric or not.
a square matrix is said to be Symmetric, if the element of the ith row and jth column is equal to
the element of the jth row and ith column.
123
245
356
OUTPUT :
ORIGINAL MATRIX
123
245
356
*/
import java.util.*;
class Symetric
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter m ");
int m = sc.nextInt();
int a[][]=new int[m][m];
System.out.println("Enter elements in the Matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}
}
/* Printing the Original Matrix */
System.out.println("The Original Matrix is ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
/* Checking whether the matrix is symmetric or not */
int flag = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j] != a[j][i])
{
flag = 1; // Setting flag = 1 when elements do not match
break;
}
}
}
if(flag == 1)
System.out.println("The given Matrix is Not Symmetric");
else
System.out.println("The given Matrix is Symmetric");
}
}
Program 6
/* write a program to create a DDA of size n*n and check whether the given matrix is a UNIQUE
matrix or not
* a UNIQUE matrix is a matrix where sum of boundary and non boundary are same*/
import java.util.*;
public class unique_matrix
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
System.out.println("ENTER n");
int n=sc.nextInt();
int a[][]=new int[n][n];
System.out.println("ENTER ARRAY ELEMENTS");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
int sumb=0,sumnb=0;
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==0||j==0||i==n-1||j==n-1)
sumb=sumb+a[i][j];
else
sumnb=sumnb+a[i][j];
}
}
if(sumb==sumnb)
System.out.println("UNIQUE MATRIX");
else
System.out.println("NOT UNIQUE MATRIX");
}
}
Program 7
/* write a program to create a DDA of size n*n and display
SUM OF BOUNDARY ELEMENTS
SUM OF NON BOUNDARY ELEMENTS
SUM OF LEFT DIAGONAL ELEMENTS
SUM OF RIGHT DIAGONAL ELEMENTS
SUM OF DIAGONAL ELEMENTS
SUM OF CORNER ELEMENTS
*/
import java.util.*;
public class ARRAYSUM
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
System.out.println("ENTER n");
int n=sc.nextInt();
int a[][]=new int[n][n];
System.out.println("ENTER ARRAY ELEMENTS");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
int sumb=0,sumnb=0,suml=0,sumr=0,sumlr=0,sumc=0;
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==0||j==0||i==n-1||j==n-1)
{
sumb=sumb+a[i][j];
}
if(i!=0&&j!=0&&i!=n-1&&j!=n-1)
{
sumnb=sumnb+a[i][j];
}
if(i==j)
{
suml=suml+a[i][j];
}
if(i+j==n-1)
{sumr=sumr+a[i][j];
}
if(i+j==n-1||i==j)
{
sumlr=sumlr+a[i][j];
}
if((i==0 && j==0)||(i==0 && j==n-1)||(i==n-1 && j==0)||(i==n-1 && j==n-1))
{
sumc=sumc+a[i][j];
}
}
}
System.out.println(" SUM OF BOUNDARY ELEMENTS="+sumb);
System.out.println("SUM OF NON BOUNDARY ELEMENTS="+sumnb);
System.out.println("SUM OF LEFT DIAGONAL ELEMENTS="+suml);
System.out.println(" SUM OF RIGHT DIAGONAL ELEMENTS="+sumr);
System.out.println("SUM OF DIAGONAL ELEMENTS="+sumlr);
System.out.println("SUM OF CORNER ELEMENTS="+sumc);
}
}
Program 8
/*Write a program in Java to enter n numbers in a double dimensional array m x n
* (where m is the number of rows and n is the number of columns) and
* find out the sum of each row and each column of a matrix.
Examples:
Input: 1, 1, 1, 1
2, 2, 2, 2
3, 3, 3, 3
4, 4, 4, 4
output 1, 1, 1, 1 4
2, 2, 2, 2 8
3, 3, 3, 3 12
4, 4, 4, 4 16
10 10 10 10
*/
import java.util.Scanner;
public class DDASum
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows m ");
int m = sc.nextInt();
System.out.print("Enter number of columns n ");
int n = sc.nextInt();
int arr[][] = new int[m][n];
System.out.println("Enter array elements");
for (int i = 0; i < m ; i++)
{
for (int j = 0; j < n ; j++)
{
arr[i][j] = sc.nextInt();
}
}
System.out.println(" Array:");
//Row-wise & Column-wise Sum
for (int i = 0; i < m; i++)
{
int rSum = 0;
for (int j = 0; j < n; j++)
{
rSum += arr[i][j];
System.out.print(arr[i][j] + "\t");
}
System.out.print(rSum);
System.out.println();
}
for (int i = 0; i < m ; i++)
{
int cSum = 0;
for (int j = 0; j < n ; j++)
{
cSum += arr[j][i];
}
System.out.print(cSum + "\t");
}
}
}
Program 9
/* 5. Define a class to accept values into a 3x3 array and check if it is a Losho grid or not.
A grid is a Losho grid if the sum of each row and column equals 15.
Print appropriate messages.
Example: 4 9 2
357
8 1 6 */
import java.util.*;
public class Losho
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[][]=new int[3][3];
int temp=0;
System.out.println("enter the array element");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
int rsum=0,csum=0;
for(int i=0;i<3;i++)
{
rsum=0;csum=0;
for(int j=0;j<3;j++)
{
rsum=rsum+a[i][j];
csum=csum+a[j][i];
}
if(rsum!=15||csum!=15)
{ temp=1;
break;
}
}
if(temp==1)
System. out. println("Not a loshogrid");
else
System. out. println("losho grid");
}
}
Question 10
[15]
Define a class to accept values into an integer array of order 4 x 4 and check whether it is
a DIAGONAL array or not. An array is DIAGONAL if the sum of the left diagonal
elements equals the sum of the right diagonal elements. Print the appropriate message.
Example:
3 4 2 5 Sum of the left diagonal elements =
2 5 2 3 3 + 5 + 2 + 1 = 11
5 3 2 7 Sum of the right diagonal elements =
1 3 7 1 5 + 2 + 3 + 1 = 11
import java.util.*;
public class Diagonal
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int arr[][] = new int[4][4];
System.out.println("Enter elements for 4x4 DDA:");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
arr[i][j] = sc.nextInt();
}
}
int lSum = 0,rSum = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if(i==j)
lSum =lSum+ arr[i][j];
if(i+j==3)
rSum =rSum+ arr[i][j];
}
}
if (lSum == rSum)
System.out.println("The array is a DIAGONAL array.");
else
System.out.println("The array is NOT a DIAGONAL array.");
}
}