---
title: "Write A program for Swapping in C# ?"  
description: "Write A program for Swapping in C# ?"  
author: "Gurmeet Kaur"  
published: 2018-09-09  
updated: 2018-09-09  
canonical: https://answers.mindstick.com/qa/49698/write-a-program-for-swapping-in-c-sharp  
category: "programming"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Write A program for Swapping in C# ?

1). With temp [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) And 2). Without Temp variable ?

## Answers

### Answer by Sanat Shukla

1). Swapping With Temporary variable

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{

    class Program
    {
        static void Main(string[] args)
        {
            int A, B, temp;
            Console.Write("\nEnter the First Number(for A) : ");
            A = int.Parse(Console.ReadLine());
            Console.Write("\nEnter the Second Number(for B) : ");
            B = int.Parse(Console.ReadLine());
            temp = A;
            A = B;
            B = temp;
            Console.Write("\nAfter Swapping : ");
            Console.Write("\nFirst Number A is : " + A);
            Console.Write("\nSecond Number B is : " + B);
            Console.Read();
        }
    }
}
```

2). Without temp variable used in swapping

1. Use + and -
2. Use *and /

## * and /

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication8{    class Program    {        public static void Main(string[] args)        {            int a = 15, b = 30;            Console.WriteLine("Before swap a= " + a + " b= " + b);            a = a * b; //a=450 (15*30)            b = a / b; //b=15 (450/30)            a = a / b; //a=30 (450/15)            Console.Write("After swap a= " + a + " b= " + b);            Console.WriteLine("\n");        }    }}
```

## + and -

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{
    class Program
    {
        public static void Main(string[] args)
        {
            int a = 30, b = 10;
            Console.WriteLine("Before swap a= " + a + " b= " + b);
            a = a + b;
            b = a - b;
            a = a - b;
            Console.Write("After swap a= " + a + " b= " + b);
            Console.WriteLine("\n");
        }
    }
}
```

## \

## \

\


---

Original Source: https://answers.mindstick.com/qa/49698/write-a-program-for-swapping-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
