Write a program to Find Norm and Trace of Matrix of Size 3 X 3 in C
Write a program to Find Norm and Trace of Matrix of Size 3 X 3 in C++
Write a program to Find Norm and Trace of Matrix of Size 3 X 3 in Python
Write a program to Find Norm and Trace of Matrix of Size 3 X 3 in PHP
Write a program to Find Norm and Trace of Matrix of Size 3 X 3 in Java
Write a program to Find Norm and Trace of Matrix of Size 3 X 3 in Java Script
Write a program to Find Norm and Trace of Matrix of Size 3 X 3 in C#
Explanation:
Steps:
Input:
Read a 3×3 matrix.
Compute Trace:
Loop through the diagonal elements of the matrix and compute their sum.
Compute Norm:
Loop through all elements of the matrix.
Compute the square of each element and calculate the sum of these squares.
Take the square root of the result to get the norm.
Output:
Display the trace and norm of the matrix.
Program to Find Norm and Trace of Matrix of Size 3 X 3
C
C++
Python
PHP
JAVA
Java Script
C#
#include <stdio.h>
#include <math.h>
int main() {
int matrix[3][3];
int trace = 0;
double norm = 0;
// Input elements for the matrix
printf("Enter elements for a 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
// Calculate trace and norm
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
trace += matrix[i][j]; // Sum diagonal elements for trace
}
norm += matrix[i][j] * matrix[i][j]; // Sum of squares for norm
}
}
norm = sqrt(norm); // Frobenius norm
// Print the result
printf("\nTrace of the matrix: %d\n", trace);
printf("Norm of the matrix: %.2f\n", norm);
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int matrix[3][3];
int trace = 0;
double norm = 0;
// Input elements for the matrix
cout << "Enter elements for a 3x3 matrix:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "Enter element [" << i + 1 << "][" << j + 1 << "]: ";
cin >> matrix[i][j];
}
}
// Calculate trace and norm
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
trace += matrix[i][j]; // Sum diagonal elements for trace
}
norm += matrix[i][j] * matrix[i][j]; // Sum of squares for norm
}
}
norm = sqrt(norm); // Frobenius norm
// Print the result
cout << "\nTrace of the matrix: " << trace << endl;
cout << "Norm of the matrix: " << norm << endl;
return 0;
}
import math
matrix = []
print("Enter elements for a 3x3 matrix:")
for i in range(3):
row = []
for j in range(3):
value = int(input(f"Enter element [{i+1}][{j+1}]: "))
row.append(value)
matrix.append(row)
# Calculate trace and norm
trace = 0
norm = 0
for i in range(3):
for j in range(3):
if i == j:
trace += matrix[i][j] # Sum diagonal elements for trace
norm += matrix[i][j] ** 2 # Sum of squares for norm
norm = math.sqrt(norm) # Frobenius norm
# Print the result
print(f"\nTrace of the matrix: {trace}")
print(f"Norm of the matrix: {norm:.2f}")
<?php
$matrix = array();
$trace = 0;
$norm = 0;
// Input elements for the matrix
echo "Enter elements for a 3x3 matrix:\n";
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$matrix[$i][$j] = (int)readline("Enter element [" . ($i+1) . "][" . ($j+1) . "]: ");
}
}
// Calculate trace and norm
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
if ($i == $j) {
$trace += $matrix[$i][$j]; // Sum diagonal elements for trace
}
$norm += $matrix[$i][$j] * $matrix[$i][$j]; // Sum of squares for norm
}
}
$norm = sqrt($norm); // Frobenius norm
// Print the result
echo "\nTrace of the matrix: $trace\n";
echo "Norm of the matrix: " . number_format($norm, 2) . "\n";
?>
import java.util.Scanner;
public class MatrixNormAndTrace {
public static void main(String[] args) {
int[][] matrix = new int[3][3];
int trace = 0;
double norm = 0;
Scanner scanner = new Scanner(System.in);
// Input elements for the matrix
System.out.println("Enter elements for a 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("Enter element [" + (i + 1) + "][" + (j + 1) + "]: ");
matrix[i][j] = scanner.nextInt();
}
}
// Calculate trace and norm
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
trace += matrix[i][j]; // Sum diagonal elements for trace
}
norm += matrix[i][j] * matrix[i][j]; // Sum of squares for norm
}
}
norm = Math.sqrt(norm); // Frobenius norm
// Print the result
System.out.println("\nTrace of the matrix: " + trace);
System.out.printf("Norm of the matrix: %.2f\n", norm);
scanner.close();
}
}
let matrix = [];
let trace = 0;
let norm = 0;
console.log("Enter elements for a 3x3 matrix:");
for (let i = 0; i < 3; i++) {
let row = [];
for (let j = 0; j < 3; j++) {
let value = parseInt(prompt(`Enter element [${i+1}][${j+1}]:`));
row.push(value);
}
matrix.push(row);
}
// Calculate trace and norm
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === j) {
trace += matrix[i][j]; // Sum diagonal elements for trace
}
norm += matrix[i][j] * matrix[i][j]; // Sum of squares for norm
}
}
norm = Math.sqrt(norm); // Frobenius norm
// Print the result
console.log(`\nTrace of the matrix: ${trace}`);
console.log(`Norm of the matrix: ${norm.toFixed(2)}`);
using System;
class MatrixNormAndTrace {
static void Main() {
int[,] matrix = new int[3, 3];
int trace = 0;
double norm = 0;
// Input elements for the matrix
Console.WriteLine("Enter elements for a 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write($"Enter element [{i+1}][{j+1}]: ");
matrix[i, j] = int.Parse(Console.ReadLine());
}
}
// Calculate trace and norm
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
trace += matrix[i, j]; // Sum diagonal elements for trace
}
norm += matrix[i, j] * matrix[i, j]; // Sum of squares for norm
}
}
norm = Math.Sqrt(norm); // Frobenius norm
// Print the result
Console.WriteLine("\nTrace of the matrix: " + trace);
Console.WriteLine("Norm of the matrix: " + norm.ToString("F2"));
}
}