Search This Blog

This website completely moved to new domain. For latest content, visit www.programmingposts.com

C# PROGRAM TO PERFORM SUBTRACTION ON MATRICES USING ARRAYS

Implementing Subtraction of Matrices Using 2D-Arrays:

using System;


namespace MatrixSubtractionProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            //using 2D-ARRAYS
            int RowSize = 5, ColSize = 5;
            int[,] Matrix1 = new int[RowSize, ColSize];
            int[,] Matrix2 = new int[RowSize, ColSize];
            int[,] ResultMatrix = new int[RowSize, ColSize];
            int i, j;


            try
            {
                Console.WriteLine("\n >>> PROGRAM To PRINT SUBTRACTION OF TWO MATRICES <<<");
                Console.Write("\n Enter the Size of a Matrix N*N (For Example:3*3) : ");
                string s = Console.ReadLine();
                string[] values = s.Split('*');
                RowSize = int.Parse(values[0]);
                ColSize = int.Parse(values[1]);

                if (RowSize > 5 || ColSize > 5)   //limiting the size of matrix
                {
                    Console.BackgroundColor = ConsoleColor.DarkRed; //changing background color to red
                    Console.WriteLine(" The Size Of Matrix should Be in Less Than 5 (limiting size of array)");
                    System.Console.ResetColor(); ///resetting color
                    Console.WriteLine("\n\n\t Press Enter key to exit....");
                    Console.ReadKey(); return;
                }

                else
                {

                    //Initializing all the elements to zero
                    for (i = 0; i < RowSize; i++)
                    {
                        for (j = 0; j < ColSize; j++)
                        {
                            Matrix1[i, j] = 0;
                            Matrix2[i, j] = 0;
                        }
                    }
                    //Reading elements of Matrix1
                    Console.WriteLine("\n Enter the elements of Matrix1({0}*{1})", RowSize, ColSize);
                    for (i = 0; i < RowSize; i++)
                    {
                        for (j = 0; j < ColSize; j++)
                        {
                            Console.Write(" Matrix1[{0},{1}] : ", i, j);
                            Matrix1[i, j] = Convert.ToInt32(Console.ReadLine());
                        }
                    }
                    //Reading elements of Matrix2
                    Console.WriteLine("\n Enter the elements of Matrix2({0}*{1})", RowSize, ColSize);
                    for (i = 0; i < RowSize; i++)
                    {
                        for (j = 0; j < ColSize; j++)
                        {
                            Console.Write(" Matrix2[{0},{1}] : ", i, j);
                            Matrix2[i, j] = Convert.ToInt32(Console.ReadLine());
                        }
                    }

                    //calculating ResultMatrix, by SUBTRACTING Matrix2 FROM Matrix1
                    for (i = 0; i < RowSize; i++)
                    {
                        for (j = 0; j < ColSize; j++)
                        {
                            ResultMatrix[i, j] = Matrix1[i, j] - Matrix2[i, j];
                        }
                    }

                    //Printing Result Matrix
                    Console.Write("\n\n\t*** Result Matrix  ***\n\n\t");
                    for (i = 0; i < RowSize; i++)
                    {

                        for (j = 0; j < ColSize; j++)
                        {
                            if (ResultMatrix[i, j] < 10)
                            {
                                //Making number as 01,02,etc,.
                                Console.Write("  0" + Convert.ToString(ResultMatrix[i, j])); 
                            }
                            else
                            {
                                Console.Write("  " + Convert.ToString(ResultMatrix[i, j]));
                            }

                            if (j == ColSize - 1) { Console.Write("\n\t"); }

                        }
                    }
                }
            }
            catch  //to catch exceptions,suppose string entered as aRowSize or Colsize  of matrix
            {
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("WARNING:only Number are allowed, Enter Correct Input");
                Console.ResetColor();
            }

            Console.WriteLine("\n\n\t Press Enter key to exit....");
            Console.ReadLine();
        }
    }
}

In the above program the code can be shortened by reducing the number of for loops or by using functions, but for making it easier to understand by the beginners, the program is made as simple as possible. Some of the different Sample Outputs of the above program are shown below:

Output:

  
For C Program :

C PROGRAM TO PERFORM SUBTRACTION ON MATRICES



 More posts: 

C# PROGRAM FOR ADDITION OF MATRICES USING ARRAYS


20 comments:

  1. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training in chennai velachery | dot net training institute in velachery

    ReplyDelete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.. dot net training and placement in chennai | best dot net training in chennai

    ReplyDelete
  3. to awesome information it pretty good about c# dot net.
    best training institute in chennai

    ReplyDelete
  4. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
    Digital Marketing online training

    full stack developer training in pune

    full stack developer training in annanagar

    full stack developer training in tambaram

    full stack developer training in velachery

    ReplyDelete
  5. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
    python training institute in chennai
    python training in chennai
    python training in chennai

    ReplyDelete
  6. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
    Blueprism training in annanagar

    Blueprism training in velachery

    Blueprism training in marathahalli

    ReplyDelete
  7. From your discussion I have understood that which will be better for me and which is easy to use. Really, I have liked your brilliant discussion. I will comThis is great helping material for every one visitor. You have done a great responsible person. i want to say thanks owner of this blog.
    Data science training in tambaram
    Data Science training in anna nagar
    Data Science training in chennai
    Data science training in Bangalore
    Data Science training in marathahalli
    Data Science training in btm

    ReplyDelete
  8. You blog post is just completely quality and informative. Many new facts and information which I have not heard about before. Keep sharing more blog posts.
    java training in chennai | java training in bangalore

    java online training | java training in pune

    ReplyDelete
  9. I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

    ReplyDelete
  10. Thank you for posting informative insights, I think we have got some more information to share with! Do check out
    oracle dba training in chennai and let us know your thoughts. Let’s have great learning!

    ReplyDelete
  11. Sharing the same interest, Infycle feels so happy to share our detailed information about all these courses with you all! Big data training in chennai & get to know everything you want to about software trainings.

    ReplyDelete
  12. Searching for the Oracle Training in Chennai? Then come to Infycle for the best software training in Chennai. Infycle Technologies is one of the best Oracle training institute in Chennai, which offers various programs in Oracle such as Oracle PLSQL, Oracle DBA, etc., in complete hands-on practical training from professionals in the field. Along with that, the interviews will be arranged for the candidates and 200% placement assurance will be given here. To have the words above in your life, call 7502633633 to Infycle Technologies and grab a free demo to know more.Top Oracle DBA Training in Chennai

    ReplyDelete
  13. Data Science training in Chennai | Infycle Technologies

    Infycle Technologies is the best software training center in Chennai, providing amazing Data Science training in Chennai which will be realistic and taught by industry experts. Aside from practical training preparation, mock interviews will be arranged for students so that they can confidently face the interviews. All of this will be packed up by full placement assurance with the best salary package from top companies. For having a free demo session, call 7502633633.
    Best training center in Chennai

    ReplyDelete
  14. Infycle Technologies, the No.1 software training institute in Chennai offers the No.1 Big Data Hadoop Training in Chennai | Infycle Technologies for students, freshers, and tech professionals. Infycle also offers other professional courses such as DevOps, Artificial Intelligence, Cyber Security, Python, Oracle, Java, Power BI, Selenium Testing, Digital Marketing, Data Science, etc., which will be trained with 200% practical classes. After the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7502633633 to get more info and a free demo.

    ReplyDelete
  15. Infycle Technologies, the best software training institute cum placement center in Chennai offers the best Digital Marketing course in Chennai for freshers, students, and tech professionals at the best offers. In addition to the Digital Marketing Training, other in-demand courses such as AWS, DevOps, Data Science, Python, Selenium, Big Data, Java, Power BI, Oracle will also be trained with 100% practical classes. After the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7504633633 to get more info and a free demo.Best Digital Marketing Course in Chennai | Infycle Technologies

    ReplyDelete
  16. Smart move for your career is Choosing to do Oracle Course in Chennai at Infycle!! Do you know why this name is chosen for Infycle. Infycle where the place we offered Infinity of Oracle.
    Yes!!! But not only Oracle, More than 20+ courses are offered here 5000+ students are placed in top MNC’s Company with good salary packages. For admission 7502633633. Oracle Course in Chennai | Infycle Technologies

    ReplyDelete