Wednesday, 24 February 2016

C# Program how to Swap two Numbers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
    class SwapProgram
    {
        static void Main(string[] args)
        {
            int number1, number2, temp;
            Console.Write("\nEnter the First Number : ");
            number1 = int.Parse(Console.ReadLine());
            Console.Write("\nEnter the Second Number : ");
            number2 = int.Parse(Console.ReadLine());
            temp = number1;
            number1 = number2;
            number2 = temp;
            Console.Write("\nAfter Swapping : ");
            Console.Write("\nFirst Number : " + number1);
            Console.Write("\nSecond Number : " + number2);
            Console.Read();
        }
    }
}
---------------
OUTPUT:

Enter the First Number : 50
Enter the Second Number : 100

After Swapping :

First Number : 100
Second Number : 50
---------------------------------------------

Saturday, 20 February 2016

C# Program how to Display Numbers of Triangle

using System;

class Triangle
{
    public static void Main()
    {
        int[,] arr = new int[8, 8];    
        for (int i = 0; i < 8; i++)
        {
            for (int k = 7; k > i; k--)
            {
                Console.Write(" ");
            }

            for (int j = 0; j < i; j++)
            {
                if (j == 0 || i == j)
                {
                    arr[i, j] = 1;
                }
                else
                {
                    arr[i, j] = arr[i - 1, j] + arr[i - 1, j - 1];
                }
                Console.Write(arr[i, j] + " ");
            }
            Console.WriteLine();

        }
        Console.ReadLine();
    }
}
-------------------------
OUTPUT:-
       1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1
 1 5 10 10 5 1
1 6 15 20 15 6 1
-----------------------------------

Tuesday, 16 February 2016

C# Program how to Check the Exist of a File

using System;
using System.IO;

class Filecheck
{
    static void Main()
    {
        FileInfo info = new FileInfo("D:\\ABV\\doc.txt");
        bool exists = info.Exists;
        if (exists == true)
        {
            Console.WriteLine("The File Exists in the drive");
        }
        else
        {
            Console.WriteLine("No Such File Found");
        }
        Console.Read();
    }
}
-----------------------
OUTPUT:-   The File Exists in the drive 
---------------------------------

Monday, 15 February 2016

C# Program how to Call Math Operations with Delegates

using System;
public class MathOperationsWithDelegate
{
    public static double Multiply(double value)
    {
        return value * 2;
    }

    public static double Square(double value)
    {
        return value * value;
    }
}


delegate double DoubleOp(double x);

class Application
{
    static void Main()
    {
        DoubleOp[] operations =
            {
               MathOperationsWithDelegate.Multiply,
               MathOperationsWithDelegate.Square
            };

        for (int i = 0; i < operations.Length; i++)
        {
            Console.WriteLine("Operation[{0}]:", i);
            ProcessAndDisplayNumber(operations[i], 3.0);
            ProcessAndDisplayNumber(operations[i], 11.44);
            ProcessAndDisplayNumber(operations[i], 1.732);
            Console.WriteLine();
        }
        Console.ReadLine();
    }

    static void ProcessAndDisplayNumber(DoubleOp action, double value)
    {
        double result = action(value);
        Console.WriteLine(
           "Value : {0}  Result : {1}", value, result);
    }
}
---------------
OUTPUT:-

Operation[0]:
Value : 3  Result : 6
Value : 11.44  Result : 22.88
Value : 1.732  Result : 3.464

Operation[1]:
Value : 3  Result : 9
Value : 11.44  Result : 130.8736
Value : 1.732  Result : 2.999824
----------------------------------------------

Sunday, 14 February 2016

C# Program how to Create Generic Delegate

using System;
using System.Collections.Generic;
delegate T NumberChanger<T>(T n);
namespace GenericDelegate
{
    class TestDelegate
    {
        static int number = 20;
        public static int AddNum(int x)
        {
            number += x;
            return number;
        }

        public static int MultNum(int y)
        {
            number *= y;
            return number;
        }
        public static int getNum()
        {
            return number;
        }

        static void Main(string[] args)
        {
            NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);
            NumberChanger<int> nc2 = new NumberChanger<int>(MultNum);
            nc1(15);
            Console.WriteLine("Value of number: {0}", getNum());
            nc2(10);
            Console.WriteLine("Value of number: {0}", getNum());
            Console.ReadKey();
        }
    }
}
-------------------------
OUTPUT:-
Value of number: 35
Value of number: 350
-------------------------------

Saturday, 13 February 2016

C Sharp Program how to impletement Jagged Arrays

using System;

class JaggedArray
{
    static void Main()
    {
        int[][] jag = new int[3][];
        jag[0] = new int[2];
        jag[0][0] = 10;
        jag[0][1] = 11;
        jag[1] = new int[1] { 10 };
        jag[2] = new int[3] { 15, 16, 18 };
        for (int i = 0; i < jag.Length; i++)
        {
            int[] innerArray = jag[i];
            for (int a = 0; a < innerArray.Length; a++)
            {
                Console.WriteLine(innerArray[a] + " ");
            }
        }
        Console.Read();
    }
}
--------------
OUTPUT:-

10
11
10
15
16
18
----------------------------

How to Find the Average Values of all the Array Elements

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

class AverageValue
{
    public void sumAverageElements(int[] arrA, int size)
    {

        int sum = 0;
        int average = 0;
        for (int x = 0; x < size; x++)
        {
            sum += arrA[x];
        }
        average = sum / size;
        Console.WriteLine("Sum Of Array is : " + sum);
        Console.WriteLine("Average Of Array is : " + average);
        Console.ReadLine();
    }
    public static void Main(string[] args)
    {
        int size;
        Console.WriteLine("Enter the Size :");
        size = Convert.ToInt32(Console.ReadLine());
        int[] y = new int[size];
        Console.WriteLine("Enter the Elements of the Array : ");
        for (int x = 0; x < size; x++)
        {
            y[x] = Convert.ToInt32(Console.ReadLine());
        }
        int len = y.Length;
        AverageValue Objavg = new AverageValue();
        Objavg.sumAverageElements(y, len);
    }
}
---------------------
OUTPUT:-
Enter the Size :
3
Enter the Elements of the Array :
4
6
7
Sum Of Array is : 17
Average Of Array is : 5
--------------------------------------------

Friday, 12 February 2016

How to Configure Routing in MVC:

0). Route must be registered in Application_Start event in Global.ascx.cs file.
1). Routing plays important role in MVC framework. Routing maps URL to physical file or class.
2). Route contains URL pattern and handler information. URL pattern starts after domain name.
3). Routes can be configured in RouteConfig class. Multiple custom routes can also be configured.

--------------------------------------------------------------------------------------
Below Example:-

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}


URL                                            Controller                     Action         Id
http://localhost/home                    Home                             Index         Null
http://localhost/home/index/786  Home                             Index         786

C Sharp Program how to Implement Pass by Value Parameter

using System;

class PassValueByParameter
{
    static void Cube(int i)
    {
        i = i * i * i;
        Console.WriteLine("Value Within the Cube method : {0}", i);
    }
    static void Main()
    {
        int num = 10;
        Console.WriteLine("Value Before the Method is called : {0}", num);
        Cube(num);
        Console.WriteLine("Value After the Method is called : {0}", num);
        Console.ReadKey();
    }
}
------------------------
OUTPUT:-
Value Before the Method is called : 10
Value Within the Cube method : 1000
Value After the Method is called : 10
------------------------------------------------------

Thursday, 11 February 2016

C Sharp Program how to use Sleep method of Thread

using System;
using System.Diagnostics;
using System.Threading;
class SleepMethod
{
    static void Main()
    {
        var stopwatch = Stopwatch.StartNew();
        Thread.Sleep(4000);
        stopwatch.Stop();
        Console.WriteLine("Elapsed Milliseconds : {0}", stopwatch.ElapsedMilliseconds);
        Console.WriteLine("Elapsed Ticks : {0}", stopwatch.ElapsedTicks);
        Console.WriteLine("Present Date and Time : {0}", DateTime.Now.ToLongTimeString());
        Console.ReadLine();
    }
}
----------------
OUTPUT:-
Elapsed Milliseconds : 3999
Elapsed Ticks : 12468015
Present Date and Time : 9:27:22 AM
-------------------------------------------

Wednesday, 10 February 2016

C# Program how to Create a Simple Thread

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

class SimpleThread
{
    public void ThreadFunction()
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Created Simple Thread");
        }
    }
}
class threprog
{
    public static void Main()
    {
        SimpleThread pg = new SimpleThread();
        Thread thread = new Thread(new ThreadStart(pg.ThreadFunction));
        thread.Start();
        Console.Read();
    }
}
--------------------
OUTPUT:-
Created Simple Thread
Created Simple Thread
Created Simple Thread
Created Simple Thread
Created Simple Thread
------------------------------------------------

C Sharp Program how to Display Results using Delegate

using System;
public class DelegateSum
{
    public delegate int DelegateResulteHandler(int x, int y);
    static void Main(string[] args)
    {
        Results Results = new Results();
        DelegateResulteHandler sum = new DelegateResulteHandler(Results.sum );
        int result = sum (30, 70);
        Console.WriteLine("Result is: " + result);
        Console.ReadLine();
    }
}

public class Results
{
    public int sum (int x, int y)
    {
        return x + y;
    }
}
-------------------------------
OUTPUT:
                    Result is: 100
---------------------------

Tuesday, 9 February 2016

C# Program How to Implement Multicast Delegates

using System;
delegate void dele(int a, int b);
public class MulDelegate
{
    public static void Add(int a, int b)
    {
        Console.WriteLine("{0} + {1} = {2}", a, b, a + b);
    }

   public static void Sub(int a, int b)
    {
        Console.WriteLine("{0} - {1} = {2}", a, b, a - b);
    }
}
public class program
{
    static void Main()
    {
        dele del = new dele(MulDelegate.Add);
        del += new dele(MulDelegate.Sub);
        del(6,3);
        del -= new dele(MulDelegate.Sub);
        del(3, 8);
        Console.Read();
    }
}
-----------------
OUTPUT:-
6 + 3 = 9
6 - 3 = 3
3 + 8 = 11
-------------------------------------

Monday, 8 February 2016

C# Program how to Demonstrate iList Interface

using System;
using System.Collections.Generic;

class Interface
{
    static void Main()
    {
        int[] a = new int[3];
        a[0] = 10;
        a[1] = 20;
        a[2] = 30;
        DisplayResult (a);

        List<int> list = new List<int>();
        list.Add(50);
        list.Add(70);
        list.Add(90);
        DisplayResult (list);
        Console.ReadLine();
    }

    static void DisplayResult (IList<int> list)
    {
        Console.WriteLine("Count: {0}", list.Count);
        foreach (int number in list)
        {
            Console.WriteLine(number);
        }
    }
}
--------------------
OUTPUT:- 
Count: 3
10
20
30
Count: 3
50
70

90
------------------------------

Sunday, 7 February 2016

How to Create a File in C# Program

using System;
using System.IO;
using System.Text;
class Demo
{
    public static void Main()
    {
        string filepath= @"d:\raj\emo.txt";
        using (FileStream fs = File.Create(filepath))
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("File is Created Successfully");
            fs.Write(info, 0, info.Length);
        }
        using (StreamReader sr = File.OpenText(filepath))
        {
            string x = "";
            while ((x = sr.ReadLine()) != null)
            {
                Console.WriteLine(x);
            }
        }
        Console.Read();
    }
}
---------------------
OUTPUT:-

File is Created Successfully
-------------------------------------------

Saturday, 6 February 2016

C# Program to Reverse an Array

using System;
class Reverse
{
    static void Main()
    {
        int[] array = { 1, 2, 3,4,5,6,7,8,9,10 };
        foreach (int a in array)
        {
            Console.WriteLine(a);
        }
        Array.Reverse(array);
        Console.WriteLine("Reversed Array : ");
        foreach (int value in array)
        {
            Console.WriteLine(value);
        }
        Console.ReadLine();
    }
}
-------------------
OUTPUT:-                          Reversed Array:
1                                           10
2                                            9
3                                            8 
4                                            7 
5                                            6
6                                            5  
7                                            4  
8                                            3
9                                            2

10                                          1 
-------------------------------------------

Friday, 5 February 2016

Char Array: Use the Sort() method

using System;

class MainClass
{

    public static void Main()
    {
        char[] MycharArray = { 'x', 'v', 'n', 'b', 'p', 'h', 'v' };
        Array.Sort(MycharArray);
        Console.WriteLine("Sorted charArray:");
        for (int r = 0; r < MycharArray.Length; r++)
        {
            Console.WriteLine("MycharArray[" + r + "] = " +
              MycharArray[r]);
        }
    }

}
---------------------
OUTPUT:-

Sorted charArray:
MycharArray[0] = b
MycharArray[1] = h
MycharArray[2] = n
MycharArray[3] = p
MycharArray[4] = v
MycharArray[5] = v
MycharArray[6] = x
-------------------------------------------

How to passed value type by reference

using System;

class RefType
{
    public void sqr(ref int i)
    {
        i = i * i;
    }
}

class MainClass
{
    public static void Main()
    {
        RefType ob = new RefType();

        int x = 20;

        Console.WriteLine("a before call: " + x);

        ob.sqr(ref x);

        Console.WriteLine("a after call: " + x);
    }
}
------------------------
OUTPUT:-
a before call: 20
a after call: 400
----------------------------------------------------

How to passed Objects by reference

using System;

class Test
{
    public int m, n;

    public Test(int i, int j)
    {
        m = i;
        n = j;
    }

    public void change(Test ob)
    {
        ob.m = ob.m + ob.n;
        ob.n = -ob.n;
    }
}

class MainClass
{
    public static void Main()
    {
        Test ob = new Test(10, 30);

        Console.WriteLine("ob.a and ob.b before call: " +
                           ob.m + " " + ob.n);

        ob.change(ob);

        Console.WriteLine("ob.a and ob.b after call: " +
                           ob.m + " " + ob.n);
    }
}
-----------------------------------
OUTPUT:-

ob.a and ob.b before call: 10 30
ob.a and ob.b after call: 40 -30
---------------------------------------------

How to Implement MobileBook

using System;
using System.Collections;
using System.IO;
class MobileBook
{
 
    static void Main(string[] arg)
    {
        Hashtable tab = new Hashtable();
        string fileName;
        if
        { 
            (arg.Length > 0) fileName = arg[0];
        } 
        else
        { 
            fileName = "MobileBook.txt";
        }
        StreamReader r = File.OpenText(fileName);
        string line = r.ReadLine();  
        while (line != null)
        {
            int pos = line.IndexOf('=');
            string name = line.Substring(0, pos).Trim();
            long Mobile= Convert.ToInt64(line.Substring(pos + 1));
            tab[name] = Mobile;
            line = r.ReadLine();
        }
        r.Close();
        for (; ; )
        { 
            Console.Write("Name : ");
            string name = Console.ReadLine().Trim();
            if (name == "") 
                break;
            object Mobile= tab[name];
            if (Mobile== null)
                Console.WriteLine("-- Not Found in Mobile Book");
            else
                Console.WriteLine(Mobile);
        }
    }
}
-------------
OUTPUT:-

Name : Kamal
8899945777
Name : Suresh
-- Not Found in Phone Book
---------------------------------

Thursday, 4 February 2016

How to Print a Diamond Using Nested Loop

using System;
class Diamond
{
    public static void Main()
    {
        int num, i, k, count = 1;
        Console.Write("Enter number of rows\n");
        num = int.Parse(Console.ReadLine());
        count = num - 1;
        for (k = 1; k <= num; k++)
        {
            for (i = 1; i <= count; i++)
                Console.Write(" ");
            count--;
            for (i = 1; i <= 2 * k - 1; i++)
                Console.Write("*");
            Console.WriteLine();
        }
        count = 1;
        for (k = 1; k <= num - 1; k++)
        {
            for (i = 1; i <= count; i++)
                Console.Write(" ");
            count++;
            for (i = 1; i <= 2 * (num - k) - 1; i++)
                Console.Write("*");
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

--------------------------------
OUTPUT:- 
Enter number of rows:-  5

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
---------------------------------------------------------------------

Wednesday, 3 February 2016

C# Program to Calculate Acceleration

using System;
class Acceleration
{
    static void Main(string[] args)
    {
        int V, T, Acc;
        Console.WriteLine("Enter the Velocity : ");
        V= int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the Time : ");
        T = int.Parse(Console.ReadLine());
        acc = V / T;
        Console.WriteLine("Acceleration : {0}", ACC);
    }
}

-------------------------
OUTPUT:-

Enter the Velocity :
20
Enter the Time :
2
Acceleration : 10

-------------------------

Tuesday, 2 February 2016

Display All the Prime Numbers Between 1 to 100

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrimeNumber
{
    class PrimeNumber
    {
        static void Main(string[] args)
        {
            bool isPrime = true;
            Console.WriteLine("Prime Numbers : ");
            for (int a = 2; a <= 100; a++)
            {
                for (int b = 2; b <= 100; b++)
                {

                    if (a != b && a % b == 0)
                    {
                        isPrime = false;
                        break;
                    }

                }
                if (isPrime)
                {
                    Console.Write("\t" +a);
                }
                isPrime = true;
            }
            Console.ReadKey();
        }
    }
}
-------------OUTPUT:----------
Prime Numbers :
      2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
------------------------------------------

Length position of string

using System;

public class StringPostion
{
   public static void Main()
   {
      String str = "Sunil";
      String toFind = "s";
      int index = str.IndexOf("s");
      Console.WriteLine("Found '{0}' in '{1}' at position {2}",
                        toFind, str, index);
   }
}
--------------------
OUTPUT:
Found 'S' in 'sunil' at Position 1
---------------------------