Saturday 16 April 2016

Write a Program to Swap 2 Nos. using Call by Reference in C#.

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

namespace ConsoleApplication12
{
    class Program
    {
        static void Swap(ref int x, ref int y)
        {
            int temp = x;
            x = y;
            y = temp;
        }
        static void Main(string[] args)
        {
            int a, b, temp;
            Console.WriteLine("Enter 1st No. ");
            a = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter 2nd No. ");
            b = int.Parse(Console.ReadLine());
           Swap(ref a, ref b);
           Console.WriteLine("Swapped Values : " + a);
           Console.WriteLine();
           Console.WriteLine("Swapped Values : " + b);
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment