Write a Program in C#
Swap - i) Without using 3rd variable
ii) Using 3rd variable
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rushal AroraPosted Dec 1, 2013, 7:35 AM
Satyapriya NayakPosted Dec 1, 2013, 5:56 AM
class SwapTwoNumbersUsingTempVariable
{
public static void Main()
{
Console.WriteLine("/****** Program to swap 2 numbers using temporary variable *******/");
int a = 10, b = 20;
Console.WriteLine(" Values before swapping a={0} , b={1}", a, b);
int tempVar = 0; //temporary variable
tempVar = a;
a = b;
b = tempVar;
Console.WriteLine(" Values after swapping a={0} , b={1}", a, b);
Console.ReadKey();
}
}
Satyapriya NayakPosted Dec 1, 2013, 5:42 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Swapp_two_number_without_3rd_var
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int a;
int b;
a = 1;
b = 2;
MessageBox.Show("Original numbers are" + a + ":"+ + b);
a = a + b;
b = a - b;
a = a - b;
MessageBox.Show("Swapping numbers are" + a + ":" + +b);
}
}
}