THANKS FOR WATCHING MY BLOG.IF ANY PROBLEM PLEASE COMMENT

Wednesday, 30 January 2013

Fibonacci series – A sample program in C#


Matrix multiplication – A sample program in C# using arrays

Description:



Here in this program, the number of rows and columns are taken as input for the given two matrix.  Arrays and For loop is used to get  the values for each row and columns of two matices . The total number of values are validated i.e. for a 3x2 matrix the total number of values are 6 (3 rows and 2 columns). The matrix multiplication is performed if the number of columns of the first matrix is equal to the number of rows of the second matrix. For loop is used to display the values in a matrix format. Try-catch (Exception) is used to handle the errors.



Sample code:



using System;

using System.Collections.Generic;

using System.Text;



namespace codeformatirxmultiplication

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                String u_a;

                string v_a;

                string u_b;

                string v_b;

                string m;

                string n;

                int[,] a = new int[4, 4];

                int[,] b = new int[4, 4];

                int[,] c = new int[4, 4];



                int i = 0;

                int j = 0;

                int k = 0;

                int l = 0;

                c[i, j] = 0;

                int no_of_rows_a = 0;

                int no_of_columns_a = 0;

                int no_of_rows_b = 0;

                int no_of_columns_b = 0;

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

                u_a = Console.ReadLine();

                no_of_rows_a = Convert.ToInt32(u_a);

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

                v_a = Console.ReadLine();

                no_of_columns_a = Convert.ToInt32(v_a);

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

                u_b = Console.ReadLine();

                no_of_rows_b = Convert.ToInt32(u_b);

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

                v_b = Console.ReadLine();

                no_of_columns_b = Convert.ToInt32(v_b);

                Console.WriteLine(" The values of A matrix is :");

                for (i = 0; i < no_of_rows_a; i++)

                {

                    for (j = 0; j < no_of_columns_a; j++)

                    {

                        m = Console.ReadLine();

                        k = Convert.ToInt32(m);

                        a[i, j] = k;

                    }

                }

                Console.WriteLine(" The values of B matrix is :");

                for (i = 0; i < no_of_rows_b; i++)

                {

                    for (j = 0; j < no_of_columns_b; j++)

                    {

                        n = Console.ReadLine();

                        l = Convert.ToInt32(n);

                        b[i, j] = l;

                    }

                }



                Console.WriteLine(" The matrix format of A is ");



                for (i = 0; i < no_of_rows_a; i++)

                {

                    for (j = 0; j < no_of_columns_a; j++)

                    {

                        Console.Write(" {0}", a[i, j]);

                    }

                    Console.WriteLine("");

                }

                Console.WriteLine(" The matrix format of B is ");

                for (i = 0; i < no_of_rows_b; i++)

                    {

                        for (j = 0; j < no_of_columns_b; j++)

                        {

                            Console.Write(" {0}",b[i,j]);

                        }

                        Console.WriteLine("");

                }

                if (v_a == u_b)

                {

                    for (i = 0; i < no_of_rows_a; i++)

                    {

                        for (j = 0; j < no_of_columns_b; j++)

                        {

                            c[i, j] = (a[i, 0] * b[0, j]) + (a[i, 1] * b[1, j]);

                            Console.Write("  ");

                        }

                        Console.WriteLine("");

                    }

                }

                else

                {

                    Console.WriteLine("The matrix multiplication cannot be performed");



                }

                Console.WriteLine(" The resultant matrix is");

                for (i = 0; i < no_of_rows_a; i++)

                {

                    for (j = 0; j < no_of_columns_b; j++)

                    {

                        Console.Write(" {0}", c[i, j]);

                    }

                    Console.WriteLine();

                }

            }



            catch (Exception e)

            {

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

            }

        }



        }

   

    }



Sample Input :

Enter the number of rows for matrix A

3

 Enter the number of columns for matrix A

2

 Enter the number of rows for matrix B

2

 Enter the number of columns for matrix B

3

 The values of A matrix is :

2

3

5

1

10

2

 The values of B matrix is :

0

3

5

1

2

3

 The matrix format of A is

 2 3

 5 1

 10 2

 The matrix format of B is

 0 3 5

 1 2 3



Sample Output:



 The resultant matrix is

 3 12 19

 1 17 28
 2 34 56

Triangle representation – A sample program in C#

Description:



This triangle representation program is used to diplay the given character in a triangular format. In this program, no of rows is taken as input and For loop is used to represent the given character in a triangle format. Conslole.WriteLine is used to display  the given character and Console.ReadLine is used to get the input (no_of_rows) value. 



Sample code:



using System;

using System.Collections.Generic;

using System.Text;



namespace codefortriangleformat

{

    class Program

    {

        static void Main(string[] args)

        {

            string j;

            int k = 0;

            int m = 1;

            int i = 1;

            int y = 0;

            int a = 0;

            int no_of_rows = 0;

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

            j = Console.ReadLine();

            no_of_rows = Convert.ToInt32(j);

            a =  (2*no_of_rows) - 1;

            y = (a-1)/2;

            for (i = 1; i <= no_of_rows; i++)

            {

                    for (a = 0; a < y; a++)

                    {

                        Console.Write("  ");

                        Console.Write(" ");

                    }

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



                {

                    Console.Write(" * ");

                   

                }

                    Console.WriteLine(" \n");

                    m = m + 2;

                    y--;

                }

            }

               

               

            }

           }







Sample Input :

Enter the number of rows: 5



Sample Output:



                                   

            *



         *  *  *



      *  *  *  *  *



   *  *  *  *  *  *  *



*  *  *  *  *  *  *  *  *




                            

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



            *    *    *

            

    *    *    *



            *    *    *




Palindrome – A sample program in C#

Description:

In this program, either string or number is taken as a input for checking  palindrome.
Palindrome is a specific name given for the word or number which shows the same name or number when reversed from right to left or vice-versa. While loop is used to check the given condition is palindrome or not. Conslole.WriteLine is used to display  the given value is palindrome or not  and Console.ReadLine is used to get the input value.  


Sample Code:

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

namespace codeforpalindrome
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1;
            int j = 0;
            int i = 0;
            Console.WriteLine(" Enter the value ");
            s1 = Console.ReadLine();
            j = s1.Length;
            while (i != (j - 1))
            {
                if (s1[i] == s1[j - 1])
                {
                    i++;
                    j = j - 1;
                }
                else
                {
                    Console.WriteLine(" The given value is not a palindrome");
                    break;
                }
            }

                if (i == (j-1))
                {
                    Console.WriteLine("The given value is a plaindrome");
                }
            }
        }
   }


Sample Input_1:
Enter the value: 

malayalam

Sample Output: The given value is a palindrome.

Sample Input_2:
Enter the value: 

sir

Sample Output: The given value is not a palindrome.


Sample Input_3:
Enter the value: 

12321

Sample Output: The given value is a palindrome.

Sample Input_4:
Enter the value: 

12345

Sample Output: The given value is not a palindrome.

Even or odd – A sample program in C#

Description:


Here in this program, The conditional statement (If statement)is used with the condition (i.e) The remainder value is zero or non zero when dividing the given value by 2, Conslole.WriteLine is used to display  the given value is even or odd and Console.ReadLine is used to get the input value.


Sample Code:

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

namespace codeforevenorodd
{
    class Program
    {
        static void Main(string[] args)
        {
            string g;
            int j = 0;
            int i;
            Console.WriteLine(" Enter the value: ");
            g = Console.ReadLine();
            i = Convert.ToInt32(g);
            j = (i % 2);
            if(j == 0)
            {
                Console.WriteLine("The given value is even ");
            }
            else
            {
                 Console.WriteLine("The given value is odd  ");
        }
    }
}
    }

Sample Input_1:
Enter the value:
88
Sample Output: The given value is even

Sample Input_2:
Enter the value:
23
Sample Output: The given value is odd