Write a C program to find the normal of a matrix using for loop. In this C matrix normal example, we use nested for loop to iterate and find the square. Next, the Mathematical sqrt function will find the matrix square root. Thus, the normal of a matrix is the square root of the sum of the items squares.
#include <stdio.h> #include <math.h> int main() { int i, j, rows, columns, normal = 0; printf("Enter Normal Matrix Rows and Columns = "); scanf("%d %d", &rows, &columns); int Norm_arr[rows][columns]; printf("Please Enter the Normal Matrix Items = \n"); for (i = 0; i < rows; i++) { for (j = 0; j < columns; j++) { scanf("%d", &Norm_arr[i][j]); } } for (i = 0; i < rows; i++) { for (j = 0; j < columns; j++) { normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]); } } float actNormal = sqrt(normal); printf("\nThe Square Of the Matrix = %d", normal); printf("\nThe Normal Of the Matrix = %.2f\n", actNormal); }
C program to find the normal of a matrix using a while loop.
#include <stdio.h> #include <math.h> int main() { int i, j, rows, columns, normal = 0; printf("Enter Rows and Columns = "); scanf("%d %d", &rows, &columns); int Norm_arr[rows][columns]; printf("Please Enter the Items = \n"); i = 0; while (i < rows) { j = 0; while (j < columns) { scanf("%d", &Norm_arr[i][j]); normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]); j++; } i++; } float actNormal = sqrt(normal); printf("\nThe Square = %d", normal); printf("\nThe Normal = %.2f\n", actNormal); }
Enter Rows and Columns = 3 3
Please Enter the Items =
10 20 30
40 50 60
70 80 90
The Square = 28500
The Normal = 168.82
This C program calculates the normal of a given matrix using the do while loop.
#include <stdio.h> #include <math.h> int main() { int i, j, rows, columns, normal = 0; printf("Enter Rows and Columns = "); scanf("%d %d", &rows, &columns); int Norm_arr[rows][columns]; printf("Please Enter the Items = \n"); i = 0; do { j = 0; do { scanf("%d", &Norm_arr[i][j]); normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]); } while (++j < columns); } while (++i < rows); float actNormal = sqrt(normal); printf("\nThe Square = %d", normal); printf("\nThe Normal = %.2f\n", actNormal); }
Enter Rows and Columns = 4 4
Please Enter the Items =
10 20 30 40
50 60 70 80
90 10 25 35
45 55 65 75
The Square = 45350
The Normal = 212.96