Monday, April 27, 2020

Addition of two User Defined Square Matrix in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class MatrixAdd
    {
        public static void Main()
        {
            Console.WriteLine("Enter Row and Columns for First Matrix separated by single space");
            int rows, cols;
            string strRowsCols = Console.ReadLine();
            rows = int.Parse(strRowsCols.Substring(0, 1));
            cols = int.Parse(strRowsCols.Substring(2, 1));
            int j = 0;

            int[,] MatrixLength = new int[rows, cols];

            Console.WriteLine("Enter First Matrix Elements in Matrix Form");
            for (int i = 0; i < rows; i++)
            {
            int[] intArrays = Array.ConvertAll(Console.ReadLine().Split(' '), a => int.Parse(a));

                foreach (var k in intArrays)
                {
                    MatrixLength[i, j] = k;
                    j = j + 1;
                }
                j = 0;
            }

        Console.WriteLine("Enter Row and Columns for Second Matrix separated by single space");
            strRowsCols = Console.ReadLine().Trim();
            rows = int.Parse(strRowsCols.Substring(0, 1));
            cols = int.Parse(strRowsCols.Substring(2, 1));


            int[,] MatrixLength1 = new int[rows, cols];
            Console.WriteLine("Enter Second Matrix Elements in Matrix Form");
            j = 0;
            for (int i = 0; i < rows; i++)
            {
            int[] intArrays = Array.ConvertAll(Console.ReadLine().Split(' '), a => int.Parse(a));

                foreach (var k in intArrays)
                {
                    MatrixLength1[i, j] = k;
                    j = j + 1;
                }
                j = 0;
            }

           Console.WriteLine("Addition of above two Matrix");
            for (int k = 0; k < rows; k++)
            {
                for (int l = 0; l < cols; l++)
                {
                    if (l < cols - 1)
                        Console.Write("{0} ", MatrixLength[k, l] + MatrixLength1[k, l]);
                    else
                        Console.Write("{0}", MatrixLength[k, l] + MatrixLength1[k, l]);
                }

                if (k < rows - 1)
                    Console.WriteLine();
            }

            Console.Read();
        }
    }
}

First Output :



Second Output :



Thursday, April 16, 2020

Binary To Decimal Conversion in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program1
    {
        static void Main(string[] args)
        {
            double sum = 0;
            Console.WriteLine("Enter the Binary Number : ");
            string strBinary = Console.ReadLine();
            int Len = strBinary.Length;
            int slen = 0;
            int DecimalPosition = 0;
            int counter = 1;
            bool flag = false;
            if (strBinary.Contains("."))
            {
                flag = true;

                DecimalPosition = strBinary.IndexOf(".");
                slen = strBinary.Substring(DecimalPosition+1,Len-DecimalPosition-1).Length;
                foreach (var u in strBinary.Substring(DecimalPosition + 1, Len - DecimalPosition - 1))
                {

                    if (u.ToString() == "1")
                    {

                        sum = sum + Math.Pow(2, ((counter) * -1));

                    }
                    counter++;
                }
            }


            if (flag == true)
            {
                slen = strBinary.Substring(0, DecimalPosition).Length;
                foreach (var s in strBinary.Substring(0, DecimalPosition))
                {
                    if (s.ToString() == "1")
                    {
                        sum = sum + Math.Pow(2, slen - 1);
                    }
                    slen--;
                }
            }

            if (flag == false)
            {
                foreach (var s in strBinary)
                {
                    if (s.ToString() == "1")
                    {
                        sum = sum + Math.Pow(2, Len - 1);
                    }
                    Len--;
                }
            }

            Console.WriteLine("Decimal Number :");
            Console.Write(sum);
            Console.Read();
        }
    }
}

First Output :



Second Output :

Tuesday, April 14, 2020

Transpose of User Defined Matrix in C#

using System;


namespace ConsoleApplication

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Enter Row and Column Size for Matrix Separated By Single Space");

            int rows, cols;

            string strRowsCols = Console.ReadLine();

            rows = int.Parse(strRowsCols.Substring(0, 1));

            cols = int.Parse(strRowsCols.Substring(2, 1));

            int j = 0;

            int[,] UserMatrix = new int[rows, cols];



            Console.WriteLine("Enter each Matrix Elements in Matrix Form");

            for (int i = 0; i < rows; i++)

            {

                int[] intArrays = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), a => int.Parse(a));

                foreach (var k in intArrays)

                {

                    UserMatrix[i, j] = k;

                    j = j + 1;

                }

                j = 0;

            }


            Console.WriteLine("Transpose Matrix");

            int r = 0;

            int c = 0;

            int temp;

            temp = rows;

            rows = cols;

            cols = temp;

            int[,] TransposeMatrix = new int[rows, cols];



            for (int k = 0; k < rows; k++)

            {

                for (int m = 0; m < cols; m++)

                {

                    TransposeMatrix[k, m] = UserMatrix[r, c];

                    r = r + 1;

                }

                r = 0;

                c = c + 1;

            }



            for (int k = 0; k < rows; k++)

            {

                for (int l = 0; l < cols; l++)

                {

                    if (l < cols - 1)

                        Console.Write("{0} ", TransposeMatrix[k, l]);

                    else

                        Console.Write("{0}", TransposeMatrix[k, l]);

                }

                if (k < rows - 1)

                    Console.WriteLine();

            }

            Console.Read();

        }

   }

}

Test Case Output 1 :

Test Case Output 2 :