Write a Java program to find the normal of a matrix using for loop. The normal of a matrix is the square root of the sum of the squares of matrix items. In this normal example, we use a nested for loop to iterate the matrix and find the matrix square. Next, the Math sqrt function will find the matrix square root.
package NumPrograms; import java.util.Scanner; public class MatrixNormal1 { private static Scanner sc; public static void main(String[] args) { int i, j, rows, columns, normal = 0; sc= new Scanner(System.in); System.out.print("Enter Normal Matrix Rows and Columns = "); rows = sc.nextInt(); columns = sc.nextInt(); int[][] Norm_arr = new int[rows][columns]; System.out.println("Please Enter the Normal Matrix Items = "); for(i = 0; i < rows; i++) { for(j = 0; j < columns; j++) { Norm_arr[i][j] = sc.nextInt(); } } for(i = 0; i < rows; i++) { for(j = 0; j < columns; j++) { normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]); } } double actNormal = Math.sqrt(normal); System.out.println("\nThe Square Of the Matrix = " + normal); System.out.println("The Normal Of the Matrix = " + actNormal); } }
Java program to find the normal of a matrix using a while loop
package NumPrograms; import java.util.Scanner; public class MatrixNormal2 { private static Scanner sc; public static void main(String[] args) { int i, j, rows, columns, normal = 0; sc= new Scanner(System.in); System.out.print("Enter Normal Matrix Rows and Columns = "); rows = sc.nextInt(); columns = sc.nextInt(); int[][] Norm_arr = new int[rows][columns]; System.out.println("Please Enter the Normal Matrix Items = "); i = 0; while(i < rows) { j = 0; while(j < columns) { Norm_arr[i][j] = sc.nextInt(); normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]); j++; } i++; } double actNormal = Math.sqrt(normal); System.out.println("\nThe Square Of the Matrix = " + normal); System.out.println("The Normal Of the Matrix = " + actNormal); } }
Enter Normal Matrix Rows and Columns = 3 3
Please Enter the Normal Matrix Items =
15 25 35
45 55 65
75 85 95
The Square Of the Matrix = 33225
The Normal Of the Matrix = 182.27726133558184
This example uses the do while loop to calculate and print the normal of a given matrix.
package NumPrograms; import java.util.Scanner; public class MatrixNormal3 { private static Scanner sc; public static void main(String[] args) { int i, j, rows, columns, normal = 0; sc= new Scanner(System.in); System.out.print("Enter Normal Matrix Rows and Columns = "); rows = sc.nextInt(); columns = sc.nextInt(); int[][] Norm_arr = new int[rows][columns]; System.out.println("Please Enter the Normal Matrix Items = "); i = 0; do { j = 0; do { Norm_arr[i][j] = sc.nextInt(); normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]); }while(++j < columns); } while(++i < rows); double actNormal = Math.sqrt(normal); System.out.println("\nThe Square Of the Matrix = " + normal); System.out.println("The Normal Of the Matrix = " + actNormal); } }
Enter Normal Matrix Rows and Columns = 3 3
Please Enter the Normal Matrix Items =
10 20 30
40 50 60
70 80 90
The Square Of the Matrix = 28500
The Normal Of the Matrix = 168.81943016134133