In this article we
will discuss with you the program for swapping two numbers without using third
variable using console application in c#.net:
Step 1: Take a new console application using c# as
a language in the Visual studio.
Step 2: Declare 2 variable int_First_No, int_Second_No and take
the 2 input numbers in these variables for swapping.
Step 3: now add the 2 variable and assign it to the
“int_First_No”, after this minus the “int_Second_No” from
this number we will get the first variable no in the Second variable. Likewise minus the “int_First_No”
from this number we will get the Second variable no in the first variable. In this way two numbers gets swapped without use of the third number.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
SwappingNumbers
{
class Program
{
static void Main(string[]
args)
{
int
int_First_No, int_Second_No;
Console.WriteLine("please the enter the two numbers for swapping");
int_First_No = int.Parse(Console.ReadLine());
int_Second_No = int.Parse(Console.ReadLine());
Console.WriteLine("Before swapping the two numbers is " + int_First_No
+ " and " + int_Second_No);
int_First_No = int_First_No +
int_Second_No;
int_Second_No = int_First_No -
int_Second_No;
int_First_No = int_First_No -
int_Second_No;
Console.WriteLine("After swapping the two numbers is " +
int_First_No + " and " +
int_Second_No);
Console.ReadLine();
}
}
}
Output:
No comments:
Post a Comment