Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Add Matrix in C
Next article icon

C Program To Find Normal and Trace of Matrix

Last Updated : 04 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Here, we will see how to write a C program to find the normal and trace of a matrix. Below are the examples:

Input: mat[][] = {{1, 2, 3},
                           {4, 5, 6},
                           {7, 8, 9}};
Output: 

  • Normal = 16
  • Trace  = 15

Explanation: 

  • Normal = sqrt(1*1+ 2*2 + 3*3 + 4*4 + 5*5 + 6*6 + 7*7 + 8*8 + 9*9) = 16
  • Trace  = 1+5+9 = 15

Input: mat[][] = {{5, 6, 1},
                          {7, 2, 9},
                          {6, 1, 3}};
Output:

  • Normal = 10
  • Trace  = 10

Explanation:

  • Normal = sqrt(5*5+ 6*6 + 1*1 + 7*7 + 2*2 + 9*9 + 6*6 + 1*1 + 3*3) = 15
  • Trace  = 5+2+3 = 10

For a better understanding see the below image.

Normal and Trace of Matrix

 

Approach:

1. To Find Normal: 

  • Run nested loop to access elements of the matrix.
  • Find the sum of all the elements present in the matrix.
  • Then return the square root of that sum.

2. To Find Trace: 

  • Run a single loop to access diagonal elements of the matrix.
  • Return the sum of diagonal elements.

Below is the C program to find normal and the trace of a matrix:

C




// C program to find trace 
// and normal of given matrix
#include <math.h>
#include <stdio.h>
  
// Returns Normal of a matrix 
// of size n x n
int findNormal(int mat[][3], 
               int n)
{
    int sum = 0;
    
    // Run nested loop to access 
    // elements of matrix
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            sum += mat[i][j] * mat[i][j];
    return sqrt(sum);
}
  
// Returns trace of a matrix of 
// size n x n
int findTrace(int mat[][3], int n)
{
    int sum = 0;
    
    // Run a loop to access diagonal 
    // elements of matrix
    for (int i = 0; i < n; i++)
        sum += mat[i][i];
    return sum;
}
  
// Driven code
int main()
{
    int mat[3][3] = {{1, 2, 3}, 
                     {4, 5, 6}, 
                     {7, 8, 9}};
    printf("Normal of Matrix = %d", 
            findNormal(mat, 3));
    printf("\nTrace of Matrix = %d", 
            findTrace(mat, 3));
    return 0;
}
 
 
Output
Normal of Matrix = 16  Trace of Matrix = 15

Time Complexity: O(n*n) 
Space Complexity: O(1)



Next Article
Add Matrix in C
author
mukulsomukesh
Improve
Article Tags :
  • C Language
  • C Programs
  • C Matrix Programs

Similar Reads

  • C Program to Find Determinant of a Matrix
    What is the Determinant of a Matrix? The determinant of a Matrix is a special number that is defined only for square matrices (matrices that have the same number of rows and columns). A determinant is used at many places in calculus and other matrices related to algebra, it actually represents the m
    7 min read
  • C Program for Kronecker Product of two matrices
    Given a [Tex]{m} imes{n} [/Tex]matrix A and a [Tex]{p} imes{q} [/Tex]matrix B, their Kronecker product C = A tensor B, also called their matrix direct product, is an [Tex]{(mp)} imes{(nq)} [/Tex]matrix. A tensor B = |a11B a12B| |a21B a22B| = |a11b11 a11b12 a12b11 a12b12| |a11b21 a11b22 a12b21 a12b22
    3 min read
  • Matrix Multiplication in C
    A matrix is a collection of numbers organized in rows and columns, represented by a two-dimensional array in C. Matrices can either be square or rectangular. In this article, we will learn the multiplication of two matrices in the C programming language. ExampleInput: mat1[][] = {{1, 2}, {3, 4}} mat
    3 min read
  • Add Matrix in C
    Matrices are the collection of numbers arranged in order of rows and columns. In this article, we will learn to write a C program for the addition of two matrices. The idea is to use two nested loops to iterate over each element of the matrices. The addition operation is performed by adding the corr
    4 min read
  • C++ Program To Find Normal and Trace of a Matrix
    Given a 2D matrix, the task is to find Trace and Normal of matrix.Normal of a matrix is defined as square root of sum of squares of matrix elements.Trace of a n x n square matrix is sum of diagonal elements.Examples : Input: mat[][] = {{7, 8, 9}, {6, 1, 2}, {5, 4, 3}}; Output: Normal = 16 Trace = 11
    2 min read
  • JavaScript Program to Find the Normal and Trace of a Matrix
    JavaScript provides us with ways to find a square matrix's normal and trace. In linear algebra, the total number of entries on the principal diagonal is indicated by the trace, while the normal represents the size of the matrix overall. It is critical to understand these concepts in a variety of com
    3 min read
  • C++ Program To Find Transpose of a Matrix
    Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, the transpose of A[][] is obtained by changing A[i][j] to A[j][i]. Example: Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. 1. For Square Matrix The below program f
    4 min read
  • Find the power of a matrix in R
    In this article, we are going to see how to compute the power of a matrix in R Programming Language. Matrix is an arrangement of numbers into rows and columns. Different ways of finding the power of matrix in R programming: By using %^%.By using a power function. Method 1: By using %^% Before using
    2 min read
  • How to Find Column Space of a Matrix
    The concept of the column space of any specific matrix may well be considered one of the simplest ideas in linear algebra and is, without doubt, one of the crucial ideas in the study of the solutions to linear systems and in the manner that promotes understanding of the effects of linear transformat
    10 min read
  • C++ Program For Determinant of a Matrix
    What is the Determinant of a Matrix? The determinant of a Matrix is a special number that is defined only for square matrices (matrices that have the same number of rows and columns). A determinant is used at many places in calculus and other matrices related to algebra, it actually represents the m
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences