THANKS FOR WATCHING MY BLOG.IF ANY PROBLEM PLEASE COMMENT

Wednesday, 30 January 2013

Matrix representation – A sample program in C#

Description:



In this program, number of rows and columns are taken as input. For loop is uesd to diplay the output in a matrix format with each values in a rows and columns as a specific character(*). Try-catch block (Exception) is used to handle errors.



Sample Code:



using System;

using System.Collections.Generic;

using System.Text;



namespace codeformatrix

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                string i;

                string j;

                int m;

                int n;

                int no_of_rows = 0;

                int no_of_columns = 0;



                Console.WriteLine("Enter the number of rows ");

                i = Console.ReadLine();

                no_of_rows = Convert.ToInt32(i);



                Console.WriteLine("Enter the number of columns ");

                j = Console.ReadLine();

                no_of_columns = Convert.ToInt32(j);

                for (n = 0; n < no_of_rows; n++)

                {



                    for (m = 0; m <  no_of_columns; m++)

                    {

                        Console.Write(" * ");

                    }

                    Console.WriteLine(" \n " );

                }



            }

            catch (Exception e)

            {

                Console.WriteLine("Exception occured: " + e.Message);



            }

        }

            }

    }





Sample Input :

Enter the number of rows: 3

Enter the number of columns: 3



Sample Output:

The matrix format for given number of rows and columns is



            *    *    *

            

    *    *    *



            *    *    *




No comments:

Post a Comment