As already familiar with C# pronounced as C Sharp programming language, we would directly understand about Swapping in C#. The word Swapping gives us the same meaning as the word in the English dictionary. It is all about the interchanging of values. Let’s get into how we can do this technique using C#.
Swapping 2 Numbers
In the interchanging of values between two different variables, we can do it in 2 ways. The first is by using a third variable also known as a temporary variable and the second method is by without using any other variable.
Let’s check below, on how we can do by using a third variable.
Code:
using System; class First { static void Main() { int a=75,b=84,t; t=a; a=b; b=t; Console.WriteLine("Values after swapping:"); Console.WriteLine("a is :"+a); Console.WriteLine("b is :"+b); } }
In the above simple program, we have taken two integer values and swapped those values using a 3rd temporary variable.
Output:
- Now let us see, How we can take the values of the variables through user input.
Code:
using System; class First { static void Main() { string a,b; int temp,c,d; Console.Write("Enter value for a :"); a = Console.ReadLine(); c = Convert.ToInt32(a); Console.WriteLine("A's value is {0}", c); Console.Write("Enter value for b : "); b = Console.ReadLine(); d = Convert.ToInt32(b); Console.WriteLine("B's value is {0}", d); temp=c; c=d; d=temp; Console.WriteLine("Values after swapping are:"); Console.WriteLine("a is : "+c); Console.WriteLine("b is : "+d); } }
Here, we have used the ReadLine method to read the user input values. And then we converted that value to an integer, else it would read it as ASCII values.
Output:
Try by removing the ToInt32 function and check how the program acts.
- Now let us see swapping of two values through the second method without using a third variable:
Code:
using System; class First { static void Main() { int a=85,b=58; a=a+b; b=a-b; a=a-b; Console.WriteLine("Values after swapping :"); Console.WriteLine("a is "+a); Console.WriteLine("b is "+b); } }
Output:
In the same way as above, as an exercise, can you try swapping two variables without using the third variable using user inputted values?
Swapping 3 Numbers
- After the successful execution of swapping of 2 numbers, let us now move on with swapping of 3 numbers.
Code:
using System; class First { static void Main() { int a=4,b=5,c=6; //swapping a=a+b+c; Console.WriteLine("After First step A value is "+a); b=a-(b+c); Console.WriteLine("After Second step B value is "+b); c=a-(b+c); Console.WriteLine("After Third step C value is "+c); a=a-(b+c); Console.WriteLine("After Fourth step A value is "+a); Console.WriteLine("Values after swapping are:"); Console.WriteLine("a is "+a); Console.WriteLine("b is "+b); Console.WriteLine("c is "+c); } }
As we already got to know the technique on how to swap numbers without using a third variable, we have used the same method in swapping of 3 numbers. For making, it clear, we have kept the console output statements after each step and took small values for the variables, so that we can understand actual mathematical calculations that are being processed more easily.
Output:
- Now, should we try using the user inputted values?
Code:
using System; clas1s First { static void Main() { string a,b,c; int x,y,z; Console.Write("Enter value for x :"); a = Console.ReadLine(); x = Convert.ToInt32(a); Console.WriteLine("X's value is {0}", x); Console.Write("Enter value for y : "); b = Console.ReadLine(); y = Convert.ToInt32(b); Console.WriteLine("Y's value is {0}", y); Console.Write("Enter value for z : "); c = Console.ReadLine(); z = Convert.ToInt32(c); Console.WriteLine("Z's value is {0}", z); x=x+y+z; Console.WriteLine("After Fourth step X value is "+x); y=x-(y+z); Console.WriteLine("After Second step Y value is "+y); z=x-(y+z); Console.WriteLine("After Third step Z value is "+z); x=x-(y+z); Console.WriteLine("After Fourth step X value is "+x); Console.WriteLine("Values after swapping are:"); Console.WriteLine("X is : "+x); Console.WriteLine("Y is : "+y); Console.WriteLine("Z is : "+z); } }
One thing we can observe here is, logic is always the same. We are just taking the dynamic values instead of hardcoded values for swapping.
Output:
As you have checked, the process of swapping is happening as:
- The x value is given to Y
- The y value is given to Z
- The z value is given to X
Perfect right? So, can we try in any other way of assigning the values? Of course, Yes.
- Quite very simple, we just need to assign Y and Z values one after the other or Y value to take the sum and again the ordering of elements can be changed. Let us see below.
Code:
width="638">using System; class First { static void Main() { string a,b,c; int x,y,z; Console.Write("Enter value for x :"); a = Console.ReadLine(); x = Convert.ToInt32(a); Console.WriteLine("X's value is {0}", x); Console.Write("Enter value for y : "); b = Console.ReadLine(); y = Convert.ToInt32(b); Console.WriteLine("Y's value is {0}", y); Console.Write("Enter value for z : "); c = Console.ReadLine(); z = Convert.ToInt32(c); Console.WriteLine("Z's value is {0}", z); x=x+y+z; Console.WriteLine("After Fourth step X value is "+x); z=x-(y+z); Console.WriteLine("After Second step Z value is "+z); y=x-(y+z); Console.WriteLine("After Third step Y value is "+y); x=x-(y+z); Console.WriteLine("After Fourth step X value is "+x); Console.WriteLine("Values after swapping are:"); Console.WriteLine("X is : "+x); Console.WriteLine("Y is : "+y); Console.WriteLine("Z is : "+z); } }
The only difference between the above programs is the highlighted part.
Output:
As noticed above, the output of swapping has changed to:
- The x value is assigned to Z
- The y value is assigned to X
- The z value is assigned to Y
We can try swapping three numbers in other different ways. So, as an exercise can you try swapping 3 variables using a fourth temporary variable?
Conclusion- Swapping in C#
Here, we have successfully made different swapping techniques using C# programming language for both the two variables and three variables. Have fun learning C#.
The above is the detailed content of Swapping in C#. For more information, please follow other related articles on the PHP Chinese website!

如何使用C#编写时间序列预测算法时间序列预测是一种通过分析过去的数据来预测未来数据趋势的方法。它在很多领域,如金融、销售和天气预报中有广泛的应用。在本文中,我们将介绍如何使用C#编写时间序列预测算法,并附上具体的代码示例。数据准备在进行时间序列预测之前,首先需要准备好数据。一般来说,时间序列数据应该具有足够的长度,并且是按照时间顺序排列的。你可以从数据库或者

如何使用Redis和C#开发分布式事务功能引言分布式系统的开发中,事务处理是一项非常重要的功能。事务处理能够保证在分布式系统中的一系列操作要么全部成功,要么全部回滚。Redis是一种高性能的键值存储数据库,而C#是一种广泛应用于开发分布式系统的编程语言。本文将介绍如何使用Redis和C#来实现分布式事务功能,并提供具体代码示例。I.Redis事务Redis

如何实现C#中的人脸识别算法人脸识别算法是计算机视觉领域中的一个重要研究方向,它可以用于识别和验证人脸,广泛应用于安全监控、人脸支付、人脸解锁等领域。在本文中,我们将介绍如何使用C#来实现人脸识别算法,并提供具体的代码示例。实现人脸识别算法的第一步是获取图像数据。在C#中,我们可以使用EmguCV库(OpenCV的C#封装)来处理图像。首先,我们需要在项目

C#开发中如何处理跨域请求和安全性问题在现代的网络应用开发中,跨域请求和安全性问题是开发人员经常面临的挑战。为了提供更好的用户体验和功能,应用程序经常需要与其他域或服务器进行交互。然而,浏览器的同源策略导致了这些跨域请求被阻止,因此需要采取一些措施来处理跨域请求。同时,为了保证数据的安全性,开发人员还需要考虑一些安全性问题。本文将探讨C#开发中如何处理跨域请

Redis在C#开发中的应用:如何实现高效的缓存更新引言:在Web开发中,缓存是提高系统性能的常用手段之一。而Redis作为一款高性能的Key-Value存储系统,能够提供快速的缓存操作,为我们的应用带来了不少便利。本文将介绍如何在C#开发中使用Redis,实现高效的缓存更新。Redis的安装与配置在开始之前,我们需要先安装Redis并进行相应的配置。你可以

如何使用C#编写动态规划算法摘要:动态规划是求解最优化问题的一种常用算法,适用于多种场景。本文将介绍如何使用C#编写动态规划算法,并提供具体的代码示例。一、什么是动态规划算法动态规划(DynamicProgramming,简称DP)是一种用来求解具有重叠子问题和最优子结构性质的问题的算法思想。动态规划将问题分解成若干个子问题来求解,通过记录每个子问题的解,

如何在C#中实现遗传算法引言:遗传算法是一种模拟自然选择和基因遗传机制的优化算法,其主要思想是通过模拟生物进化的过程来搜索最优解。在计算机科学领域,遗传算法被广泛应用于优化问题的解决,例如机器学习、参数优化、组合优化等。本文将介绍如何在C#中实现遗传算法,并提供具体的代码示例。一、遗传算法的基本原理遗传算法通过使用编码表示解空间中的候选解,并利用选择、交叉和

如何使用C#编写背包问题算法背包问题(KnapsackProblem)是一个经典的组合优化问题,它描述了一个给定容量的背包以及一系列物品,每个物品都有自己的价值和重量。目标是找到一种最佳策略,使得在不超过背包容量的情况下,装入背包的物品总价值最大。在C#中,可以通过动态规划方法来解决背包问题。具体实现如下:usingSystem;namespace


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version