KnowledgeBoat Logo
|

Computer Applications

Define a class to accept values into a 4 × 4 integer array. Calculate and print the NORM of the array.
NORM is the square root of sum of squares of all elements.

1    2   1   3
5    2   1   6
3    6   1   2
3    4   6   3

Sum of squares of elements = 1 + 4 + 1 + 9 + 25 + 4 + 1 + 36 + 9 + 36 + 1 + 4 + 9 + 16 + 36 + 9 = 201
NORM = Square root of 201 = 14.177446878757825

Java

Java Arrays

24 Likes

Answer

import java.util.Scanner;
class Norm{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int a[][] = new int[4][4];
        int sum = 0;
        System.out.println("Enter array elements:");
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                a[i][j] = in.nextInt();
            }
        }
        
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                sum += a[i][j] * a[i][j];
            }
        }
        
        double root = Math.sqrt(sum);
        System.out.println("NORM = " + root);
    }
}

Output

BlueJ output of Define a class to accept values into a 4 × 4 integer array. Calculate and print the NORM of the array. NORM is the square root of sum of squares of all elements. 1 2 1 3 5 2 1 6 3 6 1 2 3 4 6 3 Sum of squares of elements = 1 + 4 + 1 + 9 + 25 + 4 + 1 + 36 + 9 + 36 + 1 + 4 + 9 + 16 + 36 + 9 = 201 NORM = Square root of 201 = 14.177446878757825

Answered By

13 Likes


Related Questions