In this simple C# tutorial we were showing swapping between two numbers without using any third varible. Here we take "float" datatype, you can also take "int" or "double" datatype too depending on the number entered by the user. Here we are taking two variables "a" for number one and "b" for number two. To show swapping without using any third variable we are using simple mathematics,don't worry its too easy. Now, lets get started!!
using System; class sum { public static void Main(String []args) { float a,b; Console.WriteLine("Enter value of a:"); a=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter value of b:"); b=Convert.ToInt32(Console.ReadLine()); a=a+b; b=a-b; a=a-b; Console.WriteLine("After swapping value of a is "+ a ); Console.WriteLine("After swapping value of b is "+ b ); } }
0 comments:
Post a Comment