Tuesday, October 15, 2013

//Author: Suresh Babu
//To find the biggest number in array (Single dimentional)

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

namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = new int[5];
            for (int y = 0; y < arr1.Length; y++)
            {
                arr1[y] = int.Parse(Console.ReadLine());
            }
            int x = arr1.Max();
            Console.WriteLine("The biggest number of array is" + x);
        }
    }
}
           
                     
           

           
               
       


//Author: Suresh Babu
//Use of if and else to find the biggest of three numbers


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

namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, y, z;
            x = int.Parse(Console.ReadLine());
            y = int.Parse(Console.ReadLine());
            z = int.Parse(Console.ReadLine());
            if (x > y && x > z)
            {
                Console.WriteLine("The value of x is {0} is greater than both y and z", x);
            }
            else if (y > x && y > z)
            {
                Console.WriteLine("The value of y is {0} is greater than both x and z", y);
            }
            else
            {
                Console.WriteLine("The value of z is {0} is greater than both x and y", z);
            }
               
        }
    }
}

Wednesday, July 24, 2013

Printing of characters of String in Reverse Order in C Sharp

//File Name: reverse
//Author: SURESH BABU
//Reference: Printing of characters of String in Reverse Order

using System;


namespace reverse
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "SURESH BABU";
            string reverse = "";
            foreach (char ch in name)
            {
                reverse = ch +reverse;
            }
            Console.WriteLine(reverse);
        }
    }
}

Example for Copy Constructor in C Sharp

Example for copy Constructor:

//Fileno: demo_49
//Author: SURESH BABU
//Reference: Copy Constructor

using System;


namespace demo_49
{
    public class CopyConstruct
    {
        private int x, y;
        public CopyConstruct(int a, int b)
        {
            x=a;
            y=b;
        }
        public  CopyConstruct(CopyConstruct T)
        {
           x=   T.x;
           y =  T.y;
        }
        public void show()
        {
            Console.WriteLine("The value of x and y are {0} and {1}",x,y);
        }
        public void print()
        {
            Console.WriteLine("The value of a and b are {0} and {1}",x,y);
        }


    }
    class Program
    {
        static void Main(string[] args)
        {
            CopyConstruct t3 = new CopyConstruct(80,90);
           
            CopyConstruct t2 = new CopyConstruct(t3);
            t2.show();
            t2.print();
           

        }
    }
}

Monday, July 22, 2013

Copying of Array1 to Array2 in C Sharp

Example to copy the Array1 to Array2 and print the details in C Sharp

//file name: demo_40
//Author: SURESH BABU
//Reference: Copying of array from another array

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

namespace demo_40
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr1 = new int[3, 3];
            int[,] arr2 = new int[3, 3];
            int i, j;

            // Storing User input in the Array

            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    Console.WriteLine("Enter the Array Value:\t");
                    arr1[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    arr2[i, j] = arr1[i, j];
                }
            }
            Console.Write("Elements of the Array2 = {");
            for (i = 0; i < 3; i++)
            {
             
                for (j = 0; j < 3; j++)
                {
                    Console.Write(" {0}", arr2[i, j]);
                }
            }
            Console.Write(" }");
            Console.ReadLine();
        }
    }
}

Jagged Arrays

In C Sharp: Jagged Arrays are described as it is collection of arrays of different or same number of elements in it but of similar type only.

Syntax of the Jagged Array
---------------------------

int[ ][ ] <<Jagged Array variable Name>  =  new int [ 2 ][ ];

int[ ][ ] jagged_array  =  new int [ 2 ][ ];

In above syntax, jagged_array is variable of Array data type, where it is having 2 Arrays in it.

Example:

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

namespace demo_20
{
    class Program
    {
        static void Main(string[] args)
        {

            //Declare two elements of the Array

            int[][] array = new int[2][];

            //filling the elements
            array[0] = new int[5] { 1, 3, 5, 7, 9 };
            array[1] = new int[4] { 2, 4, 6, 8 };

            //Display the array elements

            for (int i = 0; i < array.Length; i++)
            {
                Console.Write("Element of Array [{0}]: ", i);
                for (int j = 0; j < array[i].Length; j++)
                {
                    Console.Write("{0}{1}", array[i][j], j == (array[i].Length - 1) ? "" : "",i,j);
                }
                Console.WriteLine();
            }    

        }
    }
}

Saturday, July 20, 2013

Structures in C Sharp


                       Structures are like classes, where as we can create objects that behave like the built –in types. They resemble most like classes even though it’s found lot of differences between them.
Primitive difference between them is


  •  Structures are value based where as classes are reference types
  • Structures do not possess Deconstruct  characteristics
  •  Inheritance is strictly not supported in structures where as in classes we do.
  •   Structures do not support OOPS concept

Example 1:

// file: demo_42.cs
//Author: SURESH BABU
//Purpose: Structure definition


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

namespace demo_42
{
    // Structure definition
    struct Fraction
    {
        public int numerator;
        public int denominator;

        public void print()
        {
            Console.WriteLine("  {0} / {1}", numerator, denominator);

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Fraction f;
            f.numerator = 5;
            f.denominator = 10;
            f.print();

            Fraction f2 = f;
            f2.print();

            //modify struct instance f2

            f2.numerator = 1;
            f.print();
            f2.print();
        }

    }
}


Example 2:
//File: demo_43
//Author: SURESH BABU
//Reference: Defining a structure to as reference class

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

namespace demo_43
{
    struct Rectangle
    {
        private int m_width;
        public int width
        {
            get { return m_width; }
            set { m_width = value; }
        }

        private int m_height;
            public int height
            {
                get{return m_height;}
                set {m_height=value;}

            }

    }
    class Program
    {
        static void Main(string[] args)
        {
            int Area;
            Area = 0;
            Rectangle rect1 = new Rectangle();
            Console.WriteLine("Enter the value of widht and height of the Rectangle");
            rect1.width = int.Parse(Console.ReadLine());
            rect1.height = int.Parse(Console.ReadLine());
            Console.WriteLine("Dimensions of Width & Height");
            Console.WriteLine("____________________");
            Area = rect1.height * rect1.width;
            Console.WriteLine("Width:         {0}"+"cms"+"\nHeight:        {1}"+"cms", rect1.width, rect1.height);
            Console.WriteLine("\nArea of the Rectangle is {0}"+" sq.cms", Area);



        }
    }
}