Showing posts with label C# Tutorials. Show all posts
Showing posts with label C# Tutorials. Show all posts

CSharp .Net Matrix Multiplication Program with Output

C# .Net Matrix Multiplication Program 

This C#.net program is about Matrix Multiplication with output and source code.For matrix multiplication we take 2D arrays and perform multiplication operations on two arrays and store the result in third array. Matrix multiplication program is compiled and executed using microsoft visual studio and .net compiler

Matrix multiplication program Logic:

  • First we declare two integer variable m and n to store the matrix dimensions entered by user
  • Declare three two dimensional arrays to store matrix A, matrix B and matrix C
  • Read matrix A using for loop and store it in array matrix a[m,n]
  • Read matrix B using for loop and store it in array matrix b[m,n]
  • Perform sum of matrix A and matrix B   using for loop and store it in array matrix c[m,n].
  • using  c[i, j] += a[i, k] * b[k, j];
  • show content of matrix a , 
  • show content of matrix b and 
  • show content matrix c as multiplication of matrix a and matrix b

Matrix multiplication program Sourcecode

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

namespace MatrixMultiplicationProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            
            int i, j,m,n;
            Console.WriteLine("Enter the Number of Rows and Columns : ");
            m = Convert.ToInt32(Console.ReadLine());
            n = Convert.ToInt32(Console.ReadLine());
            int[,] a = new int[m, n];
            Console.WriteLine("Enter the First Matrix");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    a[i, j] = int.Parse(Console.ReadLine());
                }
            }
            int[,] b = new int[m, n];
            Console.WriteLine("Enter the Second Matrix");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    b[i, j] = int.Parse(Console.ReadLine());
                }
            }

            Console.WriteLine("First matrix is:");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(a[i, j] + "\t");
                }
                Console.WriteLine();
            }
            
            Console.WriteLine("Second Matrix is :");
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    Console.Write(b[i, j] + "\t");
                }
                Console.WriteLine();
            }
            Console.WriteLine("Matrix Multiplication is :");
            int[,] c = new int[m, n];
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    c[i, j] = 0;
                    for (int k = 0; k < 2; k++)
                    {
                        c[i, j] += a[i, k] * b[k, j];
                    }
                }
            }
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(c[i, j] + "\t");
                }
                Console.WriteLine();
            }
 
            Console.ReadKey();
        }
        }
    }

Matrix Multiplication Program Output:

Enter the Number of Rows and Columns :
2
2
Enter the First Matrix
2
2
2
2
Enter the Second Matrix
2
2
2
2
First matrix is:
2       2
2       2
Second Matrix is :
2       2
2       2
Matrix Multiplication is :
8       8
8       8

CSharp Net Matrix Substraction Program with output

CSharp Matrix Substraction Program Using arrays with output

This C#.net program is about Matrix Substraction with output and source code.This matrix substraction program with output and source code is shown below. Matrix substraction program is compiled and executed using microsoft visual studio.

Matrix substraction program Logic:

  • First we declare two integer variable row and columns to store the matrix dimensions entered by user
  • Declare three two dimensional arrays to store matrixA, matrixB and matrixC
  • Read matrix A using for loop and store it in array matrixA[row,columns]
  • Read matrix B using for loop and store it in array matrixB[row,columns]
  • Perform sum of matrix A and matrix B   using for loop and store it in array matrixC[row,columns]using matrixC[j, k] = matrixA[j, k] - matrixB[j, k];
  • show content of matrixA , matrixB and matrixC as substraction of matrixA and matrixB

C# Matrix Substraction Program Code

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

namespace MatrixSubstraction
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter No. of Rows :: ");
            int row = int.Parse(Console.ReadLine());

            Console.Write("Enter No. of Columns :: ");
            int column = int.Parse(Console.ReadLine());

            int[,] matrixA = new int[row, column];
            int[,] matrixB = new int[row, column];
            int[,] matrixC = new int[row, column];

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Enter Matrix B Data :: MatrixA [ " + row + " X " + column + "]");

            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write("Enter Data for [ " + j + "," + k + "]" + " ");
                    matrixA[j, k] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Enter Matrix B Data :: MatrixB [ " + row + " X " + column + "]");

            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write("Enter Data for [ " + j + "," + k + "]" + " ");
                    matrixB[j, k] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine(Environment.NewLine);
            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    matrixC[j, k] = matrixA[j, k] - matrixB[j, k];
                }
            }

            Console.WriteLine("Matrix A is::");
            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write(matrixA[j, k] + " ");
                }

                Console.WriteLine();
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Matrix B is::");
            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write(matrixB[j, k] + " ");
                }

                Console.WriteLine();
            }
            Console.WriteLine(Environment.NewLine);


            Console.WriteLine("Substraction of Matrix A and B is: ");

            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write(matrixC[j, k] + " ");
                }

                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}

C# Matrix Substraction Program Output

Enter No. of Rows :: 2
Enter No. of Columns :: 2

Enter Matrix B Data :: MatrixA [ 2 X 2]
Enter Data for [ 0,0] 2
Enter Data for [ 0,1] 2
Enter Data for [ 1,0] 2
Enter Data for [ 1,1] 2

Enter Matrix B Data :: MatrixB [ 2 X 2]
Enter Data for [ 0,0] 1
Enter Data for [ 0,1] 1
Enter Data for [ 1,0] 1
Enter Data for [ 1,1] 1

Matrix A is::
2 2
2 2

Matrix B is::
1 1
1 1

Substraction of Matrix A and B is:
1 1
1 1

CSharp .Net Matrix Addtion Program with Output and Source code

C# .Net Matrix Addtion Program with Output and Source code

This c#.net program performs matrix addition. This matrix addition program with output and source code is shown below. Matrix addition program is compiled and executed using microsoft visual studio.

Matrix addition program Logic:


  • First we declare two integer variable row and columns to store the matrix dimensions entered by user
  • Declare three two dimensional arrays to store matrixA, matrixB and matrixC
  • Read matrix A using for loop and store it in array matrixA[row,columns]
  • Read matrix B using for loop and store it in array matrixB[row,columns]
  • Perform sum of matrix A and matrix B   using for loop and store it in array matrixC[row,columns] using matrixC[j, k] = matrixA[j, k] + matrixB[j, k];
  • show content of matrixA , matrixB and matrixC as sum of matrixA and matrixB

Matrix Addition Program Source Code

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

namespace MatrixAddition
{
    class MatrixAdditionProgram
    {
        static void Main(string[] args)
        {
            Console.Write("Enter No. of Rows :: ");
            int row = int.Parse(Console.ReadLine());

            Console.Write("Enter No. of Columns :: ");
            int column = int.Parse(Console.ReadLine());

            int[,] matrixA = new int[row, column];
            int[,] matrixB = new int[row, column];
            int[,] matrixC = new int[row, column];

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Enter Matrix B Data :: MatrixA [ " + row + " X " + column + "]");

            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write("Enter Data for [ " + j + "," + k + "]"+" ");
                    matrixA[j, k] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Enter Matrix B Data :: MatrixB [ " + row + " X " + column + "]");

            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write("Enter Data for [ " + j + "," + k + "]"+" ");
                    matrixB[j, k] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine(Environment.NewLine);
            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    matrixC[j, k] = matrixA[j, k] + matrixB[j, k];
                }
            }

            Console.WriteLine("Matrix A is::");
            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write(matrixA[j, k] + " ");
                }

                Console.WriteLine();
            }
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Matrix B is::");
            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write(matrixB[j, k] + " ");
                }

                Console.WriteLine();
            }
            Console.WriteLine(Environment.NewLine);


            Console.WriteLine("Sum of Matrix A and B is: ");

            for (int j = 0; j < row; j++)
            {
                for (int k = 0; k < column; k++)
                {
                    Console.Write(matrixC[j, k] + " ");
                }

                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}

Matrix Addition Program Output

Enter No. of Rows :: 2
Enter No. of Columns :: 2

Enter Matrix B Data :: MatrixA [ 2 X 2]
Enter Data for [ 0,0] 2
Enter Data for [ 0,1] 2
Enter Data for [ 1,0] 2
Enter Data for [ 1,1] 2

Enter Matrix B Data :: MatrixB [ 2 X 2]
Enter Data for [ 0,0] 1
Enter Data for [ 0,1] 1
Enter Data for [ 1,0] 1
Enter Data for [ 1,1] 1

Matrix A is::
2 2
2 2

Matrix B is::
1 1
1 1

Sum of Matrix A and B is:
3 3
3 3

csharp dotnet break keyword example

C# .net Program Break Keyword Example

  • This C# .net Program is about use Break Keyword.
  • Break Keyword is used to break the flow of program and throw the control outside the loop or conditional statement.
  • Break keyword will bring the control out of current loop in which it is written not out of all the loops.
  • Break Keyword in c# can be used with
  • For Loop
  • Swith Case
  • While Loop
  • Do While Loop
  • If Condition
  • If Else Condition

C# Program Logic:

  • In below c# program we have used while loop to show use of break keyword
  • The initial condition of while loop is set to true
  • The integer variable num is initially set to zero and incremented by 1 inside the while loop
  • Inside the while loop we have used if condition to check when the break statement should be executed.
  • In this program when the num variable is equals to 5 then the break statement is executed and controls comes out of while loop

Break Keyword C# Program

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

namespace BreakProgram
{
    class Program
    {
        static void Main()
        {
            int num = 0;
            while (true)
            {
                //Increment the number by 1
                num++;
                Console.WriteLine(num);
                //if num is 5 , break the loop
                if (num == 5)
                {
                    Console.WriteLine("Break Statement Executed.");
                    break;
                }
            }
            Console.WriteLine("Program End.");
            Console.ReadLine();
        }

    }
}

Output of Break Program

1
2
3
4
5
Break Statement Executed.
Program End.

c# program convert array list to array example

C# Program to convert ArrayList to Array Example

  • This csharp program is about how to convert array list to array
  • In this c# Program first we have created ArrayList Object and named it as arrayList
  • In order to use ArrayList in our program we have to import System.Collections namespace.
  • After creating arrayList we have added items to the arrayList Object using arrayList.Add() method.
  • To store arrayList items to an array we have created an string array object.
  • Finally using  arrayList.ToArray(typeof(string)) as string[]; we have converted Arraylist to an array.
  • Following is the program to convert ArrayList to Array
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ArrayListToArrayProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList arrayList = new ArrayList();
            arrayList.Add("AAAAAAAA");
            arrayList.Add("BBBBBBBB");
            arrayList.Add("CCCCCCCC");
            arrayList.Add("DDDDDDDD");

            string[] array = arrayList.ToArray(typeof(string)) as string[];

            Console.WriteLine("Contents of Array are:");
            foreach (var item in array)
            {
                Console.WriteLine(item);
            }
        }
    }
}



Output of Arraylist to array program.

Contents of Array are:
AAAAAAAA
BBBBBBBB
CCCCCCCC
DDDDDDDD
Press any key to continue 


csharp program to find n prime numbers in c# dotnet

Prime number program in csharp: c# program for prime number

  • The following code code prints prime numbers using csharp .net programming language. 
  • Logic for Primer Number is shown below in the code snippet with output: 
  • A number is prime if it is divisible only by one and itself. 
  • Number two is the only even and also the smallest prime number. 
  • Some example prime numbers are 2, 3, 5, 7, 11, 13, 17....etc. 
  • Every student learning programming in csharp .net will definately have to write the basic program to find prime numbers as it helps to develop the logical mind needed for programming
  • The following program will ask you to enter a number 
  • After taking the input number it will display the number of prime numbers
  • if you enter 10 the program will show the first 10 prime numbers
Prime Number Code:
 
 using System;

 namespace primerNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, i = 3, count, c;

            Console.WriteLine("Enter the number of prime numbers you want");
            num =Convert.ToInt32( Console.ReadLine());

            if (num >= 1)
            {
               
                Console.WriteLine("First "+num+" Prime Numbers are :\n");
                Console.WriteLine("2");
            }

            for (count = 2; count <= num; )
            {
                for (c = 2; c <= i - 1; c++)
                {
                    if (i % c == 0)
                        break;
                }
                if (c == i)
                {
                    Console.WriteLine("\n"+i);
                    count++;
                }
                i++;
            }
        }
    }
}



After writing this program hit F5 or cltr+F5 .
you will see a black window .program running 
Output:- 
Enter the number of prime numbers you want
5
First 5 Prime Numbers are:

3
5
7
11
Live Demo:

C# Program to Reverse a string in csharp.net

C# Program to Reverse a string in csharp.net 

  • Start Visual Studio  and select File -- New project  
  • From the project dialog, select the Console application 
  • In the Console Application in Program.cs  File write the below program to reverse a string in (c#)csharp .net 
  • In this reverse string program in csharp we are taking a string as user input
  • After taking the user input string we are looping using for loop for string.length times and appending the reverse string to a string variable and finally we are displaying the output 
  • you can refer below program for code and with live demo and output

 using System;

 namespace ReverseSample
 {
            class StringReverse
            {
                public static void Main()
                {

                    string Str, Revstr = ""; 
                    Console.Write("Enter A String : ");
                    Str = Console.ReadLine();

                    for (int i = Str.Length - 1; i >= 0; i--)
                    {

                        Revstr = Revstr + Str[i];
                    }

                    Console.WriteLine("Reverse  String  Is  {0}", Revstr);
                    Console.ReadLine();
                }
           }
   }
After writing this program hit F5 .you will see a black window .program running 

Output:- 

Enter A String : ABC 
Reverse String Is: CBA
Live Demo