logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C# Program to Check If a Given Matrix is an Identity Matrix

Posted on October 5, 2013 by Manish

This is a C# Program to check if a given matrix is an identity matrix.

Problem Description

This C# Program Checks If a Given Matrix is an Identity Matrix.

Problem Solution

Here an identity matrix is a square matrix, of size n × n, where the diagonal elements are all 1s, and the other elements are all 0s.

advertisement
Program/Source Code

Here is source code of the C# Program to Check If a Given Matrix is an Identity Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Check If a Given Matrix is an Identity Matrix
 */
using System;
class pro
{
    public static void Main()
    {
        Console.WriteLine("Enter the order: ");
        int n = int.Parse(Console.ReadLine());
        int[,] a = new int[3, 3];
        int i, j;
        Console.WriteLine("\n Enter the matrix\n");
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                a[i, j] = Convert.ToInt16(Console.ReadLine());
            }
        }
 
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                if ((i == j && a[i, j] != 1) || (i != j && a[i, j] != 0))
                {
                    goto label;
                }
            }              
        }
    Console.WriteLine("Identity Matrix");
    return;
label:
    Console.WriteLine("\n Not an Identity Matrix");
    }
}
Program Explanation

In this C# program, we are reading the order of the matrix using ‘n’ variable. Using for loop we are entering the elements for matrix. An identity matrix is a square matrix with 1’s along the diagonal from upper left to lower right and 0’s in all other positions.

advertisement

If condition statement is used to check that the matrix element a[0][0], a[1][1], a[2][2] is one, remaining element values are zero using AND logical operator. If the condition is true, then print the statement as identity matrix. Otherwise, if the condition is false then execute the else statement and print as not identity matrix.

Runtime Test Cases
 
Enter the Order : 2
Enter the Matrix :
1
0
0
1
Identity Matrix

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.
« Prev Page - C# Program to Calculate nPr
» Next Page - C# Program to Implement Traversal in Singly Linked List

« Best Reference Books – Plastic Deformation and Microstructure Evolution
C# Program to Implement Traversal in Singly Linked List »

Deep Dive @ Sanfoundry:

  1. C Programming Examples
  2. C Programming Examples on Bitwise Operations
  3. C Programming Examples on Linked List
  4. Python Programming Examples
  5. C Programming Examples on Trees
  6. C Programming Examples on Arrays
  7. C Programming Examples on Stacks & Queues
  8. Java Programming Examples on Arrays
  9. C# Programming Examples on Matrix
  10. C Programming Examples on Matrix
Manish Bhojasia
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.