Wednesday, 26 December 2012

Swapping of two numbers with using third variable and without using third variable

I. With Using Third variable

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

namespace swap
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, swap;
            Console.WriteLine("Eneter 'a' value");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter 'b' value");
            b = Convert.ToInt32(Console.ReadLine());
            swap = a;
            a = b;
            b = swap;
            Console.WriteLine("a== " + a + ",  b== " + b);
            Console.ReadLine();
        }
       
    }
}

II. without using third variable

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

namespace swap
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, swap;
            Console.WriteLine("Eneter 'a' value");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter 'b' value");
            b = Convert.ToInt32(Console.ReadLine());
            a = a + b;
            b = a - b;
            a = a - b;
            Console.WriteLine("a== " + a + ",  b== " + b);
            Console.ReadLine();
        }
       
    }
}

2 comments:

  1. You r doing a great job plz can you tell me how to work with dataview also ?

    ReplyDelete