The Program in C# Program to Swap two Numbers is given below:
using System;
namespace SwapTwoNumbers
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Famuly, Enter the first number and Second number here to Swap their numbers: ");
Console.WriteLine("Well,enter the first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Similarly,enter the second number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("This is before swapping: ");
Console.WriteLine("num1: " + num1 + " num2: " + num2);
int temp = num1;
num1 = num2;
num2 = temp;
Console.WriteLine("This is after swapping: ");
Console.WriteLine("num1: " + num1 + " num2: " + num2);
}
}
}
Output:
Hello Codeauri Famuly, Enter the first number and Second number here to find their sum:
Well, enter the first number:
20550226
Similarly,enter the second number:
20560423
Okay, the sum of 20550226 and 20560423 is: 41110649
Pro-Tips💡
Here are the step by step execution of the above program:
- The code starts by importing the System namespace.
- The namespace
SwapTwoNumbers
is created. - A class named
Program
is created within the namespace. - A
Main
method is created within theProgram
class. - The program starts by displaying a message to the user to enter two numbers.
- The first number entered by the user is stored in the
num1
variable after converting it to an integer. - The second number entered by the user is stored in the
num2
variable after converting it to an integer. - The original values of
num1
andnum2
are displayed. - A temporary variable
temp
is created and is assigned the value ofnum1
. - The value of
num1
is then updated with the value ofnum2
. - The value of
num2
is then updated with the value oftemp
. - Finally, the values of
num1
andnum2
are displayed after swapping.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.