TechieClues TechieClues
Updated date Sep 11, 2023
In this code snippet, we will see how to Swap 2 Numbers in C#. This C# program provides an easy way to sway 2 numbers using temp variable.
  • 2.2k
  • 0
  • 0

In this code snippet, we will see how to Swap 2 Numbers in C#. This C# program provides an easy way to sway 2 numbers using temp variable.

using System;
namespace SampleProgram
{
    class SwapNumbers
    {
        static void Main(string[] args)
        {
            // Declaration
            int number1;
            int number2;
            int temp;
            // Capture input
            Console.Write("\nEnter the First Number : ");
            number1 = int.Parse(Console.ReadLine());
            Console.Write("\nEnter the Second Number : ");
            number2 = int.Parse(Console.ReadLine());
            // Print numbers before swaping
            Console.Write("\nBefore Swapping : ");
            Console.Write("\nFirst Number : " + number1);
            Console.Write("\nSecond Number : " + number2);
            Console.Write("\n\n");
            // Swap numbers using temp variable
            temp = number1;
            number1 = number2;
            number2 = temp;
            // Print numbers after swaping
            Console.Write("\nAfter Swapping : ");
            Console.Write("\nFirst Number : " + number1);
            Console.Write("\nSecond Number : " + number2);
            Console.ReadKey();
        }
    }
}

Output:

Enter the First Number : 25

Enter the Second Number : 60

Before Swapping :
First Number : 25
Second Number : 60


After Swapping :
First Number : 60
Second Number : 25

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!