

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swap two numbers in C#
To swap two numbers, work with the following logic.
Set two variables for swapping −
val1 = 100; val2 = 200;
Now perform the following operation for swap −
val1 = val1 + val2; val2 = val1 - val2; val1 = val1 - val2;
The following is the code −
Example
using System; namespace Demo { class Program { static void Main(string[] args) { int val1,val2; val1 = 100; val2 = 200; Console.WriteLine("Values before swap..."); Console.WriteLine(val1.ToString()); Console.WriteLine(val2.ToString()); val1 = val1 + val2; val2 = val1 - val2; val1 = val1 - val2; Console.WriteLine("Values after swap..."); Console.WriteLine(val1.ToString()); Console.WriteLine(val2.ToString()); Console.ReadLine(); } } }
- Related Questions & Answers
- C++ Program to Swap Two Numbers
- Java Program to Swap Two Numbers.
- 8085 program to swap two 8-bit numbers
- Java program to swap two numbers using XOR operator
- How to swap two numbers without using a temp variable in C#
- 8085 program to swap two 8 bit numbers using Direct addressing mode
- 8085 program to swap two 16-bit numbers using Direct addressing mode
- Write a Golang program to swap two numbers without using a third variable
- Swap data between two columns in MySQL?
- Java program to swap two integers
- C program to swap two strings
- Swap two variables in one line in Java
- Swap two variables in one line using C#
- How to Swap Two Variables using Python?
- Swap two variables in one line in C/C+
Advertisements