DDA For rt2
DDA For rt2
Write a program to find the sum of all even numbers and product of all odd numbers of the
elements stored in Double Dimensional Array (DDA).
Sample Input:
12 10 15 17
40 11 42 71
17 14 29 41
41 44 40 51
Sample Output:
Sum of all even numbers: ……….
Product of all odd numbers: ……….
*/
import java.util.*;
public class sum_prod
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[][]=new int[4][4];
System.out.println("enter the array element");
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
}
}
int sum=0,prod=1;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(a[i][j]%2==0)
sum=sum+a[i][j];
else
prod=prod*a[i][j];
}
}
System.out.println("Sum of all even numbers: "+sum);
System.out.println("Product of all odd numbers: "+prod);
}}
//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);
}}
/* 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();
}
}
}
/* 4. 1. 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(" 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();
}
if(flag == 1)
System.out.println("The given Matrix is Not Symmetric");
else
System.out.println("The given Matrix is Symmetric");
}
}
/* 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");
}
}
/*7. write a program to create a DDA of size n*n and check whether the given matrix is a special
matrix or not
* a special matrix is a matrix where sum of left diagonal and right diagonal are same*/
import java.util.*;
public class special
{
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 suml=0,sumr=0;
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
suml=suml+a[i][j];
if(i+j==n-1)
sumr=sumr+a[i][j];
}
}
if(sumr==suml)
System.out.println("SPECIAL MATRIX");
else
System.out.println("NOT SPECIAL MATRIX");
}
}
/* 8. 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);
}
}
/* 9. Write a program to declare a matrix a[][] of order (n*n) to store integer numbers
• Display the original matrix.
• Sort each row of the matrix in ascending order using any standard sorting technique.
• Display the changed matrix after sorting each row.
*/
import java.util.*;
public class row_wise
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter array size");
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();
}
}
System.out.println("array elements");
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int temp=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
for(int k=0;k<n-1-j;k++)
{
if(a[i][k]>a[i][k+1])
{
temp=a[i][k];
a[i][k]=a[i][k+1];
a[i][k+1]=temp;
}
}
}
}
System.out.println("Sorted array elements");
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
/*10. Write a program to declare a matrix a[][] of order (n*n) to store integer numbers
• Display the original matrix.
• Sort each column of the matrix in ascending order using any standard sorting technique.
• Display the changed matrix after sorting each column.*/
import java.util.*;
public class column_wise
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter array size");
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();
}
}
System.out.println("array elements");
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int temp=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
for(int k=0;k<n-1-j;k++)
{
if(a[k][i]>a[k+1][i])
{
temp=a[k][i];
a[k+1][i]=a[k+1][i];
a[k+1][i]=temp;
}
}
}
}
System.out.println("Sorted array elements");
for (int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
/*5. 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();
}