C# Swap | C# Program To Swap Two Numbers - c# - c# tutorial - c# net




Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

How to write Swap Program in C# ?

  • When the values of two variables are exchanged at runtime it is called swapping of the two values.
  • learn c# tutorials - swap numbers in csharp - c# programs

    learn c# tutorial - swap numbers in csharp - c# programs - c# Example

  • We can swap two numbers without using third variable.
  • There are two common ways to swap two numbers without using third variable:
    1. By + and -
    2. By * and /
 swap number program c-sharp

Program 1: Using ∗ and /

  • Let's see a simple C# example to swap two numbers without using third variable.
using System;  
  public class SwapExample  
   {  
    public static void Main(string[] args)  
      {  
       int a=5, b=10;            
       Console.WriteLine("Before swap a= "+a+" b= "+b);    
       a=a*b; //a=50 (5*10)      
       b=a/b; //b=5 (50/10)      
       a=a/b; //a=10 (50/5)    
       Console.Write("After swap a= "+a+" b= "+b);       
     }  
  }
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

Before swap a= 5 b= 10     
After swap a= 10 b= 5

Program 2: Using + and -

  • Let's see another example to swap two numbers using + and -.
using System;  
  public class SwapExample  
   {  
     public static void Main(string[] args)  
      {  
       int  a=5, b=10;            
       Console.WriteLine("Before swap a= "+a+" b= "+b);    
       a=a+b; //a=15 (5+10)      
       b=a-b; //b=5 (15-10)      
       a=a-b; //a=10 (15-5)   
       Console.Write("After swap a= "+a+" b= "+b);       
      }  
  }
click below button to copy the code. By - c# tutorial - team
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

C# examples - Output :

Before swap a= 5 b= 10     
After swap a= 10 b= 5


Related Searches to C# Swap | C# Program To Swap Two Numbers

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×