Hi friends,anyone wanted to know the c# swap numbers programs in simple way can go through this

//C# program to enter two number and swap the number
using System;
class Program

static void Main(string[] args)
{
int a, b, temp;
Console.Write("Enter a value : ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter b value : ");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Before Swap a={0} b={1}\n", a, b);
temp = a;
a = b;
b = temp;
Console.Write("After Swap a={0} b={1}", a, b);
}
}

Output
Enter a value : 5
Enter b value : 10
Before Swap a=5 b=10
After Swap a=10 b=5

As i am a beginner i got to know the simple and with easy solutions to learn c++,c,c# programs with online compiler from online free tutorials.

Edited 3 Years Ago by pritaeas: Removed link. Please refrain from spam.

A good attempt. Some improvements can be made which should help you progress your programming knowledge.

Firstly when converting anything from one type to another we need some form of exception handling in place. Otherwise if I entered a letter your program would fall over.

Here we can make use of the bool int32.TryParse(string, out int); function to check if the string entered into the console can be converted to an integer.

Using the following code block we can do this:

Console.Write("Enter First Value: ");
while (!Int32.TryParse(Console.ReadLine(), out ValueOne))
{
    Console.WriteLine("Please enter a numeric value!");
    Console.WriteLine();
    Console.Write("Enter First Value: ");
}

We get the first value off the user. The .TryParse then attempts to convert the Console.ReadLine() to an integer, with the out parameter being set to the value of the successful convert.

This method also returns a bool depending on successs or failure. Therefore I can use this to control the while loop that enables multiple entries of the value until it successfully converts to an integer.

One other thing I picked up on was your use of the newline character at the end of your Console.Write() calls. There is infact a method to do this for you called Console.WriteLine() which will automatically drop to a new line at the end of its sentence.

For example:

Console.WriteLine("Before swap ValueOne: {0}; ValueTwo: {1}", ValueOne, ValueTwo);

Here would be the full code for what you did including error handling and the tidy up of Console.Write():

static void Main(string[] args)
{
    int ValueOne, ValueTwo, TempStore;

    Console.Write("Enter First Value: ");
    while (!Int32.TryParse(Console.ReadLine(), out ValueOne))
    {
        Console.WriteLine("Please enter a numeric value!");
        Console.WriteLine();
        Console.Write("Enter First Value: ");
    }

    Console.Write("Enter Second Value: ");
    while (!Int32.TryParse(Console.ReadLine(), out ValueTwo))
    {
        Console.WriteLine("Please enter a numeric value");
        Console.WriteLine();
        Console.Write("Enter Second Value: ");
    }

    Console.WriteLine("Before swap ValueOne: {0}; ValueTwo: {1}", ValueOne, ValueTwo);
    TempStore = ValueOne;
    ValueOne = ValueTwo;
    ValueTwo = TempStore;
    Console.WriteLine("After swap ValueOne: {0}; ValueTwo: {1}", ValueOne, ValueTwo);
}

Feel free to ask anything about the above, will happily explain further/clarify

Edited 3 Years Ago by Mike Askew

Comments
Fine explanation.
This article has been dead for over six months. Start a new discussion instead.