Swap two values with out using third variable.
using System;
class swap
{
public static void Main(string[] args)
{
int a,b;
Console.WriteLine("Enter the value for a");
a=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the value for b");
b=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Before swaping:-");
Console.WriteLine("a=" +a);
Console.WriteLine("b="+b);
a=a+b;
b=a-b;
a=a-b;
Console.WriteLine("After swaping:-");
Console.WriteLine("a=" +a);
Console.WriteLine("b="+b);
Console.Read();
}
}
output
before:-
a=1
b=2
After:-
a=2
b=1
No comments:
Post a Comment