C# : Program to show swapping of two numbers without using third variable

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 );
 }
}
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment