### C# Basic

### C# Interview Programs

- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number

# C# Program to Swap Two Numbers

Photo Credit to CodeToFun

## π Introduction

In the world of programming, performing basic operations on numbers is a fundamental task. Swapping two numbers is one such operation that involves exchanging the values of two variables. This operation is commonly used in various algorithms and programs.

In this tutorial, we will explore a simple C# program designed to swap the values of two numbers.

## π Example

Let's delve into the C# code that accomplishes this task.

```
using System;
class Program {
// Function to swap two numbers
static void SwapNumbers(ref int a, ref int b) {
// Using tuple deconstruction to swap values
(a, b) = (b, a);
}
// Main method
static void Main() {
// Replace these values with the numbers you want to swap
int num1 = 5, num2 = 10;
Console.WriteLine($"Before swapping: num1 = {num1}, num2 = {num2}");
// Call the function to swap the numbers
SwapNumbers(ref num1, ref num2);
Console.WriteLine($"After swapping: num1 = {num1}, num2 = {num2}");
}
}
```

### π» Testing the Program

To test the program with different numbers, modify the values of num1 and num2 in the Main method.

Before swapping: num1 = 5, num2 = 10 After swapping: num1 = 10, num2 = 5

Compile and run the program to see the result of swapping the two numbers.

### π§ How the Program Works

- The program defines a function SwapNumbers that takes two integer parameters, a and b, and swaps their values using tuple deconstruction.
- Inside the Main method, replace the values of num1 and num2 with the numbers you want to swap.
- The program prints the values of num1 and num2 before and after swapping.

## π§ Understanding the Concept of Swapping Two Numbers

Swapping two numbers involves exchanging their values. In the provided program, a temporary variable is used to hold the value of one variable while the values of the two variables are exchanged.

## π’ Optimizing the Program

While the provided program is a straightforward way to swap two numbers, consider exploring and implementing alternative approaches or optimizations.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!.

## π¨βπ» Join our Community:

## Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee
If you have any doubts regarding this article (

C# Program to Swap Two Numbers), please comment here. I will help you immediately.