5 C Sharp Programs to Swap Numbers

This post contains a total of 5 C Sharp Program Examples with Source Code to Swap Numbers. All these programs to Swap Numbers are made using C Sharp.

You can use the source code of these examples with credits to the original owner.

Related Posts

C Sharp Programs to Swap Numbers

1. By Ernest Brayce

Made by Ernest Brayce. Program to Swap Two Numbers Without using Third Number. Source

Enter first Number
First Number = 1
Enter second Number
First Second = 2
Now doing the computation ------- :::::: *******
First Number -> 2
Second Number -> 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            
           Console.WriteLine("Enter first Number");
           
           int Fnum = Convert.ToInt32(Console.ReadLine());
           
           Console.WriteLine("First Number = {0}",Fnum);
           
           Console.WriteLine("Enter second Number");
           
           int Snum = Convert.ToInt32(Console.ReadLine());
           
           Console.WriteLine("First Second = {0}",Snum);
           
           
           Console.WriteLine("Now doing the computation ------- :::::: *******");
           
           Fnum = Fnum + Snum ;
           
           Snum = Fnum - Snum;
           
           Fnum = Fnum - Snum;
           
           
           Console.WriteLine("First Number -> {0}",Fnum);
           
           Console.WriteLine("Second Number -> {0}",Snum);
           
            
        }
    }
}

2. By Sahil jangid

Made by Sahil jangid. C Sharp program to Swap Numbers and Strings. Source

9 4
World Hello
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Swap<T>(ref T a, ref T b) {
            T temp = a;
            a = b;
            b = temp;
        }
        static void Main(string[] args)
        {
            int a = 4, b = 9;
            Swap<int>(ref a, ref b);
            Console.WriteLine(a+" "+b);
            
            string x = "Hello";
            string y = "World";
            Swap<string>(ref x, ref y);
            Console.WriteLine(x+" "+y);
        }
    }
}

3. By Chungnhi

Made by Chungnhi. Program to Swap 2 number without using addition variable. Source

a = 3
b = 8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 8;
            int b = 3;

            a = a ^ b;
            b = a ^b;
            a= a^b;

            Console.WriteLine("a = " + a.ToString());
            Console.WriteLine("b = " + b.ToString());
        }
    }
}

4. By Kyaw Zin Hlaing

Made by Kyaw Zin Hlaing. Simple C Sharp program to Swap Two Numbers. Source

Enter the First Number : 2
Enter the Second Number : 3
After Swapping : 
First Number : 3
Second Number : 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2, temp;
            Console.Write("\nEnter the First Number : ");
            num1 = int.Parse(Console.ReadLine());
            Console.Write("\nEnter the Second Number : ");
            num2 = int.Parse(Console.ReadLine());
            temp = num1;
            num1 = num2;
            num2 = temp;
            Console.Write("\nAfter Swapping : ");
            Console.Write("\nFirst Number : "+num1);
            Console.Write("\nSecond Number : "+num2);
            Console.Read();
        }
    }
}

5. By Luke McMahon

Made by Luke McMahon. Number Swap Program without using a temp variable. Source

A = 5, B = 2567
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2567;
            int b = 5;
            
            a ^= b;
            b ^= a;
            a ^= b;
            
            Console.WriteLine(string.Format("A = {0}, B = {1}", a, b));
        }
    }
}