Swapping Program in C#

 

 

 

 1) Swapping two numbers using a third variable.

using System;
namespace SwappingPrograms
{
public class Program
{
public static void Main()
{
int number1 = 10, number2 = 20, temp = 0;
Console.WriteLine($"Before SWapping number1= {number1}, number2 = {number2}");
temp = number1; //temp=10
number1 = number2; //number1=20
number2 = temp; //number2=10
Console.WriteLine($"After swapping number1= {number1}, number2 = {number2}");
Console.ReadLine();
}
}
}
 
 
2)  Swap two integers without using the third variable.
using System;
namespace SwappingPrograms
{
    public class Program
    {
        public static void Main()
        {
            int number1 = 10, number2 = 20;
            Console.WriteLine($"Before SWapping number1= {number1}, number2 = {number2}");
            number1 = number1 * number2; //number1=200 (10*20)      
            number2 = number1 / number2; //number2=10 (200/20)      
            number1 = number1 / number2; //number1=20 (200/10)    
            Console.WriteLine($"After swapping number1= {number1}, number2 = {number2}");
            Console.ReadLine();
        }
    }
}
Output :
 
 

 

DOT NET ADDA

interested in solving the problems based on technologies like Amazon AWS ,Google Cloud, Azure and Dot related technology like asp.net, C#, asp.net core API, swagger, react js,Jquery ,javascripts, bootstrap, css,html, ms sql,IIS,WPF ,WCF,Firebase,RDLC Report etc..

Post a Comment (0)
Previous Post Next Post